I have one issue, to make custom field non-required field. I use Magee Restaurant plugin for Wordpress, and when try to checkout, there is one field named "Table Number". For default that field is required. How to make non-required?
Label is named:
<p class="form-row form-row-wide validate-required" id="mgrt_table_num_dine_field" data-priority=""><label for="mgrt_table_num_dine" class="">Table Number <abbr class="required" title="required">*</abbr></label><input type="text" class="input-text " name="mgrt_table_num_dine" id="mgrt_table_num_dine" placeholder="Insert your table number." value=""></p>
Thanks.
Good thing that plugin is using woocommerce_form_field function. In that case we can use it's filter. This should do it.
add_filter( 'woocommerce_form_field_args', 'magee_table_number_field_args', 10, 3 );
function magee_table_number_field_args( $args, $key, $value ){
if ( $key == 'mgrt_table_num_dine' ) {
$args['required'] = false;
}
return $args;
}
Related
I want to add mobile number field( as mandatory field) to my woocommerce store.
Is there any hard coded method to enable to same on login/registration form? Also will it be shown on the my account page(post successful creation of the account)?
Thanks IN advance.
you need to put below code in functions.php of current active theme.
/**
* To add WooCommerce registration form custom fields.
*/
function text_domain_woo_reg_form_fields() {
?>
<p class="form-row form-row-first">
<label for="billing_phone_number"><?php _e('First name', 'text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_phone_number" id="billing_phone_number" value="<?php if (!empty($_POST['billing_phone_number'])) esc_attr_e($_POST['billing_phone_number']); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');
/**
* To validate WooCommerce registration form custom fields.
*/
function text_domain_woo_validate_reg_form_fields($username, $email, $validation_errors) {
if (isset($_POST['billing_phone_number']) && empty($_POST['billing_phone_number'])) {
$validation_errors->add('billing_phone_number_error', __('<strong>Error</strong>: First name is required!', 'text_domain'));
}
return $validation_errors;
}
add_action('woocommerce_register_post', 'text_domain_woo_validate_reg_form_fields', 10, 3);
/**
* To save WooCommerce registration form custom fields.
*/
function text_domain_woo_save_reg_form_fields($customer_id) {
//First name field
if (isset($_POST['billing_phone_number'])) {
update_user_meta($customer_id, 'phone_number', sanitize_text_field($_POST['billing_phone_number']));
update_user_meta($customer_id, 'billing_phone_number', sanitize_text_field($_POST['billing_phone_number']));
}
}
add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');
I need to generate CF7 fields from php to make the form dynamic.
So instead of writing <div>[text* your-name]</div> and so on directly in wordpress, I have a template file that does that instead. The problem is that when doing so, the validation is not working. The fields are displayed correctly, but no fields are checked when submitting. If submitting, the data is however saved correctly.
WP ADMIN form tab:
[my_cf_template]
template.php
<?php echo do_shortcode(apply_filters("the_content", '[contact-form-7 id="115" title="Ruumide rent - booking"]')); ?>
functions.php
function cf_template_func(){
$email = wpcf7_do_shortcode('[email* your-email]');
$submit = wpcf7_do_shortcode( '[submit "Send"]' );
$str = <<<HTML
<div class="detailed-info">
<label> Your E-mail*
$email </label>
</div>
$submit
HTML;
return $str;
}
add_action( 'wpcf7_init', 'custom_add_shortcode');
function custom_add_shortcode() {
wpcf7_add_shortcode( 'my_cf_template', 'cf_template_func');
}
Having an issue whereby the checkboxes inside a metabox aren't saving. I can save one value and have it return the value of checked to the checkboxes, just not multiple values, which I need. I've tried adding a foreach loop to the update_post_meta code but it didn't work. Slightly confused as to where I'm going wrong.
checkbox code is:
$areas = $wpdb->get_results("SELECT * FROM locations ORDER BY locationName ASC");
if( count($areas) ) :
?>
<div id="locationAssignedBoxes" size="1">
<?php
foreach($areas as $area) :
?>
<input type="checkbox" id="locationAssigned" name="locationAssigned" value="<?php echo $area->id; ?>"<?php if(get_post_meta($post->ID, 'locationAssigned', true) == $area->id) { ?> checked="checked"<?php } ?> /> <?php echo $area->locationName; ?><br>
<?php
endforeach;
?>
</div>
<?php
endif;
?>
Update_post_meta code is:
update_post_meta($post->ID, 'locationAssigned', $_POST['locationAssigned']);
Thanks very much!
This isn't a cut-and-paste answer to your question, but it should help. (I'm trying to solve a similar problem.)
Your problem in general: update_post_meta() saves a single meta value, not a collection of values. Because you want to save the values of multiple checkboxes, you have two choices:
Call update_post_meta() once for each of the checkboxes, creating a
collection of meta values associated with the post.
Combine all of the checkbox values into a single string and save those as a single value with one update_post_meta() call.
These two earlier questions are related and might point you in the right direction.
Save checkboxes of a metabox with a foreach loop (invalid argument)
Listing Pages With Checkboxes In a Metabox (and saving them)
I want to add a class to the form, not form items. I've looked on http://codex.wordpress.org/Function_Reference/comment_form but there is no mention of adding a class to the form.
UPDATE:
Wordpress finally supports the possibility to add classes to the comments form. See Nabil Kadimi's answer to get an example.
My outdated answer:
Because Wordpress still hasn't supported this option, I've worked out the following workaround:
<?php
ob_start();
comment_form();
echo str_replace('class="comment-form"','class="comment-form your-custom-class"',ob_get_clean());
?>
Now the standard class comment-form will be replaced by itself plus the custom class.
In the documentation for the comment_form() function:
WordPress 4.4.0 Introduced the 'class_form' [...] arguments.
So you would do:
// Output the comment form with a custom class:
comment_form ( array( 'class_form' => 'my_custom_class' ) );
One a second thought
I prefer using hooks:
/**
* Callback function for the `comment_form_defaults` filter hook
*
* #param Array $defaults Defaults.
* #return Array Defaults modified.
*/
function se_8476425_modify_comment_form_defaults( $defaults ) {
$defaults[ 'class_form' ] = 'class1 class2 class3';
return $defaults;
};
add_filter( 'comment_form_defaults', 'se_8476425_modify_comment_form_defaults' );
This solution is more generic as you can use it to modify the default function behavior and themes you don't "own".
Since wordpress versiĆ³n 4.1 (Dec, 2014) the comment_form function allows to specify a class attribute for the submit button.
Php Code:
$comments_args = array('class_submit' => 'btn btn-default');
comment_form($comments_args);
Resultant HTML Button code:
<input name="submit" type="submit" id="submit" class="btn btn-default" value="Submit" />
For reference see the related ticket: https://core.trac.wordpress.org/ticket/20446
You can easily modify the code of the submit button of the coment form with this filter :
function custom_submit_comment_form( $submit_button ) {
return '<input name="submit" type="submit" id="submit" class="btn btn_2" value="Laisser un commentaire" />';
}
add_filter( 'comment_form_submit_button', 'custom_submit_comment_form' );
You can just edit your single.php and wrap the :
<?php comments_template(); ?>
In a class. Something like:
<div class="myClass">
<?php comments_template(); ?>
</div>
I want to have changeable widget title via Options Panel in wp-admin > Appearance > Widgets.
It doesn't seem to work, after clicking "SAVE" it always gives back the default instead of saving stuff.
Widgets control panel is very simplistic:
function myplugin_control() {
echo '<p>
<label for="myplugin_title">Title:</label>
<input id="myplugin_title" name="myplugin_title" type="text" value="Default title:"/>
</p>
<p>
<label for="myplugin_number">Number of items to show:</label>
<input id="myplugin_number" name="myplugin_number" type="text" value="5" size="3"/>';
$myplugin_title = ($_POST["myplugin_title"]);
$myplugin_number = ($_POST["myplugin_number"]);
update_option('myplugin_widget', $myplugin_number , $myplugin_title);
}
And plugin goes like:
(...)
function widget_myplugin($args) {
extract($args);
echo $before_widget;
echo $before_title . $myplugin_title . $after_title;
myplugin();
echo $after_widget;
}
I think you're using update_option(); improperly. It only takes two values. http://codex.wordpress.org/Function_Reference/update_option
Try change the name of your title field to simply "title". I think WP looks for this by default; see: http://wordpress.org/support/topic/how-can-i-set-a-widgets-title-in-for-use-in-the-dashboard
Instead of using $_POST['title'], use the more standard $this->get_field_id('title'); and echo $this->get_field_name('title');
Hope this helps! Also: you may find the following link helpful: http://wpengineer.com/1023/wordpress-built-a-widget/