Custom woocommerce registration form - wordpress

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');

Related

How to make form non required?

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;
}

Wordpress Contact form 7, fields in template.php - not validating correctly

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');
}

I am developing plugin give following error while activation gives

The plugin generated 2 characters of unexpected output during activation. If you notice “headers already sent” messages, my code is below
<?php
/*
Plugin Name: configuration
Description: example plugin to demonstrate wordpress capatabilities
Author: kailash
License: Public Domain
*/
// run the install scripts upon plugin activation
function test_init() {
if(isset($_POST['save'])) {
$setting=$_POST['setting'];
global $wpdb;
$table_name ="setting";
$wpdb->query("UPDATE $table_name SET setting='$setting' WHERE id=1");
//echo"Setting Saved";
}
?>
<form name="form" id="myform" method="POST">
<input type="radio" name="setting" value="2" <?php if($setting==2){echo 'checked="checked"';}?> id="term"/>newconfiguration<br>
<input type="radio" name="setting" value="1" <?php if($setting==1){echo 'checked="checked"';}?> id="term"/>oldconfiguration<br>
<input type="submit" name="save" value="save">
<?php
}
add_action('admin_menu', 'config_plugin_setup_menu');
function config_plugin_setup_menu(){
add_menu_page( 'Test Plugin Page', 'configuration', 'manage_options', 'config-plugin', 'test_init' );
}
Headers already sent
It is usually because there are spaces, new lines, or other garbage before an opening <?php tag or after a closing ?>
check and remove all unnecessary space. example check before
<form> tag. means after ?> tag there is extra space in your code. so check other too and remove all space..!
Check that the very first characters are
<?php
Check that the very last characters are
?>
for details check this
https://codex.wordpress.org/Answers-Troubleshooting

posting a footer contact form to a different script not working

I have two files the one which hosts my actual contact form and then a file where i post the form to.
contactform.php (which is part of the footer template)
<form id="contact" action="<?php bloginfo('template_url'); ?>/sendmail.php" method="post">
<label for="name">Your name: *</label>
<input type="text" id="nameinput" name="name" value=""/>
<label for="email">Your email: *</label>
<input type="text" id="emailinput" name="email" value=""/>
<label for="comment">Your message: *</label>
<textarea cols="20" rows="7" id="commentinput" name="comment"> </textarea><br />
</form>
sendmail.php
<?PHP
if(isset($_POST['submit'])) {
error_reporting(E_NOTICE);
function valid_email($str)
{
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*#([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
if($_POST['name']!='' && $_POST['email']!='' && valid_email($_POST['email'])==TRUE && strlen($_POST['comment'])>1)
{
$to = preg_replace("([\r\n])", "", hexstr($_POST['receiver']));
$from = preg_replace("([\r\n])", "", $_POST['email']);
$subject = "Website contact message from ".$_POST['name'];
$message = $_POST['comment'];
$match = "/(bcc:|cc:|content\-type:)/i";
if (preg_match($match, $to) ||
preg_match($match, $from) ||
preg_match($match, $message)) {
die("Header injection detected.");
}
$headers = "From: ".$from."\r\n";
$headers .= "Reply-to: ".$from."\r\n";
if(wp_mail($to, $subject, $message, $headers,'',true))
{
echo 1; //SUCCESS
}
else {
echo 2; //FAILURE - server failure
}
}
else {
echo 3; //FAILURE - not valid email
}
}else{
die("Direct access not allowed!");
}
function hexstr($hexstr) {
$hexstr = str_replace(' ', '', $hexstr);
$hexstr = str_replace('\x', '', $hexstr);
$retstr = pack('H*', $hexstr);
return $retstr;
}
?>
The issue is that this does not know of wp_mail function. I know that I need to include something so wp_mail will be available but what do I add? The function does exist. The issue with including the file that has wp_mail defined is that inside that function it requires some core php functions (wp_mail is being overwritten by cimy_swift plugin)
hi why not try just submitting the form to the base wpurl? then within your header.php file copy and paste your code in?
ie: using a hidden field you can check to see if its been posts, in this case the hidden field is called 'action' and it has a value of 'sendemail'.
form
<form id="contact" action="<?php bloginfo('wpurl'); ?>" method="post">
//form stuff
<input type="hidden" name="action" value="sendemail" />
</form>
Header.php
within the header file we do a call to check and see if the form has been posted,
<html>
<head>
<title><?php wp_title();?></title>
<?php
if( isset($_POST['action']) && ($_POST['action']=='sendemail') ) {
// run your code
}
?>
</head>
if you dont want to go down that route, and wish to use your theme folder to hold the php script then what to is, include the below in your sendmail.php file
define('WP_USE_THEMES', FALSE);
require('../../../wp-blog-header.php');
//above is assuming your file is located in the theme root, not a sub folder.
this will give you access to all the wordpress functions and shortcodes etc..etc..
hope that helps a little..
Marty

Adding class to the comment form in Wordpress

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>

Resources