I'm using this piece of code to add a form to a Wordpress taxonomy:
function albums_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'albums' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter a value for this field','albums' ); ?></p>
</div>
<?php
}
add_action( 'albums_add_form_fields', 'albums_taxonomy_add_new_meta_field', 10, 2 );
The value is saving fine. However, how can I output on my template the value user filled out in the form ? What's the php function to use this value in front-end ?
Thanks.
Just taking a guess, on the page where the form is being redirected to try:
echo $_POST['term_meta']['custom_term_meta']
Related
The bounty expires in 12 hours. Answers to this question are eligible for a +50 reputation bounty.
Nik7 is looking for an answer from a reputable source.
I try to auto-expand the coupon field on the WooCommerce checkout page. By default, the customer has to click "Do you have a coupon? Click here!". But we would like to have that field always visible. I tried with js but it is not working. However, I would like to have a pure PHP approach if this is possible.
add_action( 'wp_footer', 'woocommerce_show_coupon', 99 );
function woocommerce_show_coupon() {
echo '
<script type="text/javascript">
jQuery(document).ready(function($) {
$(\'.checkout_coupon\').show();
});
</script>
';
}
Does someone have an idea how I can do that in a smart way?
Cheers
//Adding CSS inline style to an existing CSS stylesheet
function mujuonly_add_inline_css() {
$mustlogin_custom_css = "
.woocommerce-form-coupon {
display:block !important;
}
";
//Add the above custom CSS via wp_add_inline_style
wp_add_inline_style( 'woocommerce-inline', $mustlogin_custom_css ); //Pass the variable into the main style sheet ID
}
add_action( 'wp_enqueue_scripts', 'mujuonly_add_inline_css' ); //Enqueue the CSS style
You can overridden by copying it to yourtheme/woocommerce/checkout/form-coupon.php the code below:
if ( ! wc_coupons_enabled() ) { // #codingStandardsIgnoreLine.
return;
}
?>
<form class="checkout_coupon woocommerce-form-coupon" method="post" style="display:block">
<p><?php esc_html_e( 'If you have a coupon code, please apply it below.', 'woocommerce' ); ?></p>
<p class="form-row form-row-first">
<label for="coupon_code" class="screen-reader-text"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label>
<input type="text" name="coupon_code" class="input-text" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />
</p>
<p class="form-row form-row-last">
<button type="submit" class="button<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_html_e( 'Apply coupon', 'woocommerce' ); ?></button>
</p>
<div class="clear"></div>
</form>
add_filter( 'woocommerce_coupons_enabled', '__return_true' );
add_filter( 'woocommerce_checkout_coupon_message', '__return_false' );
add_action( 'wp_head', 'expand_checkout_coupon_field' );
function expand_checkout_coupon_field() {
echo '<style>.checkout_coupon { display: block !important; }</style>';
}
One way to achieve this is to override the form-coupon.php template file that's responsible for rendering the coupon field on the checkout page. Here's how you can do it:
Create a new folder named woocommerce in your theme directory if it doesn't already exist.
Copy the form-coupon.php file from the wp-content/plugins/woocommerce/templates/checkout directory to the woocommerc directory in your theme.
Edit the form-coupon.php file and replace the code inside the div with the checkout_coupon class with the following code:
<div class="checkout_coupon">
<?php wc_print_notice( apply_filters( 'woocommerce_checkout_coupon_message', esc_html__( 'Have a coupon?', 'woocommerce' ) . ' ' . esc_html__( 'Click here to enter your code', 'woocommerce' ) . '' ), 'notice' ); ?>
<form class="checkout_coupon_form" method="post" style="display:block">
<p class="form-row form-row-first">
<input type="text" name="coupon_code" class="input-text" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />
</p>
<p class="form-row form-row-last">
<button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_html_e( 'Apply coupon', 'woocommerce' ); ?></button>
</p>
<div class="clear"></div>
</form>
</div>
This will show the coupon field by default on the checkout page without requiring the customer to click the "Do you have a coupon? Click here!" link.
Keep in mind that modifying the template files directly may be overwritten if WooCommerce is updated, so it's a good practice to create a child theme and modify the template file there instead.
In a nutshell what I'm trying to achieve is to let a user add some admin settings fields to a custom plugin via the backend and then populate them as required.
So the default settings page would have one Settings Field:
Location (with setting location_id) : (User enters location here and it gets stored in the database)
What I want to make happen is have a button that says "Add Another Location" which will automatically create a new settings field with an ID of location_id2. Adding a third would create a third field and the ID assigned sequentially. (Ideally I would like to give the option to remove any of the fields but that is a secondary requirement.)
These fields would be saved in the database so I can call them later in the front end using get_option('location_id');.
The code I have I currently have is for a single settings field
add_action('admin_menu', 'mycustom_menu');
function mycustom_menu() {
//create new top-level menu
add_menu_page('mycustom Connect', 'mycustom Connect', 'administrator', __FILE__, 'mycustom_settings_page' , plugins_url('/images/icon1.png', __FILE__) );
//call register settings function
add_action( 'admin_init', 'register_mycustom_settings' );
}
function register_mycustom_settings() {
//register our settings
register_setting( 'mycustom-settings-group', 'location_id' );
}
function mycustom_settings_page() {
?>
<div class="wrap">
<h1><strong></strong>mycustom Settings</strong></strong></h1>
<form autocomplete="off" method="post" action="options.php">
<?php settings_fields( 'mycustom-settings-group' ); ?>
<?php do_settings_sections( 'mycustom-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Location ID</th>
<td><input autocomplete="off" type="text" name="location_id" value="<?php echo esc_attr( get_option('location_id') ); ?>" /></td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php }
Using this same code I can manually create many of these fields as I want but as the question states how can i let a backend user do this dynamically. I have searched through quite a few other questions and haven't been able to find anything that would even help me get started so any help would be greatly appreciated.
many thanks in advance!
I read different articles, but they describe more other things. I do not want to make the wrong code with too much space. In the module there will be one single value that can be changed in the admin panel and other extraneous functions. How correctly to create a database for value of this window in a plugin?
I found the documentation, but I do not have enough experience to cut the excess, if it is.
My code /plugins/custom-counter/custom-counter.php
/*
Plugin Name: Custom Counter
Plugin URI: https://example.com
Description: This plugin adds counter.
Author: Kuznetsova Alexandra
Author URI: https://example.com
*/
// Hook for adding admin menus
add_action('admin_menu', 'custom_counter_menu');
// action function for above hook
function custom_counter_menu() {
// Add a submenu to Woocommerce menu:
add_submenu_page('woocommerce', 'Custom Counter', 'Custom Counter', 'administrator', 'custom-counter', 'custom_counter_page');
}
// custom_counter_page() displays the page content
function custom_counter_page() {
?>
<div class="wrap">
<h2>Custom Counter</h2>
<form method="post" action="options.php">
<?php settings_fields( 'baw-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">Total Counter</th>
<td><input type="text" name="custom-counter" value="..." /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php }
Updated - As this is a unique value that will be updated multiple times, you can use the wp_options table with the following WordPress dedicated functions:
get_option($option); where $option is the name of option to retrieve
update_option($option, $new_value); where $option is the name of option and $new_value the new value to be updated.
The fact that options are cached is not a problem if you use some tricks as you will see below, to avoid this data to be cached.
So try the following code:
// Add a custom admin submenu to Woocommerce
add_action('admin_menu', 'custom_counter_menu');
function custom_counter_menu() {
add_submenu_page('woocommerce', 'Custom Counter', 'Custom Counter', 'administrator', 'custom-counter', 'custom_counter_page');
}
// Content for the custom Woocommerce admin submenu
function custom_counter_page() {
$option_name = 'wc-custom-counter' ;
if( isset($_POST[$option_name]) ){
$new_value = sanitize_text_field( $_POST[$option_name] );
if ( get_option( $option_name ) !== false ) {
update_option($option_name, $new_value );
} else {
add_option( $option_name, $new_value, null, 'no' );
}
}
$default = ''; // Set the default value
$value = get_option( $option_name ) ? get_option( $option_name ) : $default;
?>
<div class="wrap">
<h2><?php _e('Custom Counter'); ?></h2>
<form method="post" action="">
<?php settings_fields( 'baw-settings-group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Total Counter'); ?></th>
<td><input type="text" name="<?php echo $option_name; ?>" value="<?php echo $value; ?>" /></td>
</tr>
</table>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
</form>
</div>
<?php
}
Code goes in function.php file of the active child theme (or active theme). tested and work.
If the option already exist, to avoid caching issue, you should need to delete it using:
delete_option('wc-custom-counter');
And adding it in your function.php file. Then browse any page of your web site and remove it.
If you only want to store one thing, like a setting, then you can use wp_options. You can then use WordPress functions to interact with the database instead of writing your own SQL.
I'm using a small form (Gravity Forms) on my site which asks for first name, last name and e-mail. After sending the form it redirects to the WooCommerce registration page and adds some parameters to the URL.
The URL looks like this: https://example.com/?first_name=First&last_name=Name&email=info%40example.com
Now I've changed the WooCommerce Template form-login.php with this code (for the e-mail field):
<?php $first_name = $_GET['first_name']; ?>
<?php $last_name = $_GET['last_name']; ?>
<?php $email = $_GET['email']; ?>
<div class="form-group">
<label for="reg_email"><?php _e( 'Email address', 'woocommerce' ); ?> <span class="required">*</span></label>
<input type="email" class="form-control" name="email" id="reg_email" value="<?php if ( ! empty( $_POST['email'] ) ) echo esc_attr( $_POST['email'] ); ?><?php if ( !empty($email) ) : echo $email; endif; ?>" placeholder="info#example.com" />
</div>
It works for this field because I can change it. (Is it ok how it's done?)
The problem is, I can't see the fields for first and last name in the template.
Therefore I don't know how to change them or add the parameter.
Is there any trick?
I could solve the problem... My mistake!
I've added the fields for first and last name like in this tutorial: https://businessbloomer.com/woocommerce-add-first-last-name-account-register-form/
Now I could add the paramaters like above.
I would like to create a custom form for my WordPress blog which takes the user's email address and then adds it to a database. I know how to write the form and script to achieve the data storage. I don't know how I would go about sticking it on WordPress blog though.
Are there any plugins for this type of thing, or is there a way I can manually add the form to the page?
It's basically a signup for notifications box.
Thanks.
You can just add it using the text widget if your theme is widget ready
Look under appearances>widgets
You can add html to text widget
If you are using more than html you'll run into problems with the widget. Upon which I'd recommend that you create a widget yourself.
Here is code for a blank plugin. Add/call your code in the "widget" function.
<?php
/*
Plugin Name: Blank Plugin
Plugin URI: http://www.example.com/plugins/blankPlugin/
Description: This is a plugin template
Author: Your Name
Version: 0.1
Author URI: http://www.example.com/about/
*/
class blankPlugin extends WP_Widget {
function blankPlugin() { // The widget construct. Initiating our plugin data.
$widgetData = array( 'classname' => 'blankPlugin', 'description' => __( "A blank plugin widget" ) );
$this->WP_Widget('blankPlugin', __('Blank Plugin'), $widgetData);
}
function widget($args, $instance) { // Displays the widget on the screen.
extract($args);
echo $before_widget;
echo $before_title . $instance['title'] . $after_title;
echo 'The amount is: '.$instance['amount'];
echo $after_widget;
}
function update($new_instance, $old_instance) { // Updates the settings.
return $new_instance;
}
function form($instance) { // The admin form.
$defaults = array( 'title' => 'Wichita', 'amount' => '45' );
$instance = wp_parse_args($instance, $defaults); ?>
<div id="blankPlugin-admin-panel">
<p>
<label for="<?php echo $this->get_field_id("title"); ?>">Widget title:</label>
<input type="text" class="widefat" name="<?php echo $this->get_field_name("title"); ?>" id="<?php echo $this->get_field_id("title"); ?>" value="<?php echo $instance["title"]; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id("amount"); ?>">An amount:</label>
<input type="text" class="widefat" name="<?php echo $this->get_field_name("amount"); ?>" id="<?php echo $this->get_field_id("amount"); ?>" value="<?php echo $instance["amount"]; ?>" />
</p>
</div>
<?php }
}
// Register the widget.
add_action('widgets_init', create_function('', 'return register_widget("blankPlugin");'));
?>
For more information... (see links at the bottom of the page as well)
http://codex.wordpress.org/Widgets_API#Developing_Widgets
you can use this wordpress plugin http://wordpress.org/extend/plugins/contact-form-7/
or can do on your own base.
you can create a template inside your theme folder like this:-
<?php
/*Template Name: some thing*/
//your dynamic stuff here
?>
and can assign this template to your static page which you can create from the admin panel of wordpress. whenever you hit the paramalink of this static page this file will be called.
thus you can handle the everything mail content etc.
Thanks.