Add Extra Fields in WordPress Registration Form




Add Extra Fields in WordPress Registration Form

Add Extra Fields in WordPress Registration Form

Now a day WordPress is a popular blogging platform and the popularity of WordPress is increasing day by day. Someone wants to create multiply author blog site. So to create a multi-author blog site or professional web site need to create a powerful WordPress registration form. If you look at the WordPress default registration form you will see like the below image.

WordPress Registration Form

We can customize the registration form for our blog site and our client’s web site. When you create your client website as a freelancer you will face any problem. Suppose your client wants to add some another field like Phone, Name with their WordPress registration form. What will you do? Don’t worry we can customize our user registration form and add some new field such as Password, Country, Fax No, Address, zip code, etc. extra fields by editing our functions.php file. It’s very simple. Simply add the below PHP codes snippets in your functions.php file. It will add the Name and Phone text field with your default author or user registration form.

<?php
// This function add form field on registration page
add_action('register_form','show_custom_field');

// This function add validation
add_action('register_post','check_fields',10,3);

// This function calls insert entry
add_action('user_register', 'register_extra_fields');

// declare the function
function show_custom_field() {?>

<p>
  <label>Name *
    <input id="custom_user_name" type="text" size="25" value="<?php echo $_POST['custom_user_name']; ?>" name="custom_user_name"/>
  </label>
</p>
<p>
  <label>Mobile
    <input id="custom_user_phone" type="text" size="25" value="<?php echo $_POST['custom_user_phone']; ?>" name="custom_user_phone"/>
  </label>
</p>
<?php }

// validation function
function check_fields($login, $email, $errors) {
global $custom_user_name, $custom_phone;
if ($_POST['custom_user_name'] == '') {
$errors->add('empty_realname', "<strong>Error:</strong> Please enter Name.");
}
else {
$custom_user_name = $_POST['custom_user_name'];
}
$custom_user_phone = $_POST['custom_user_phone'];
}
// This makes an entry into database amazing...

function register_extra_fields($user_id, $password="", $meta=array())  {
// Gotta put all the info into an array
$userdata = array();
$userdata['ID'] = $user_id;

// Name
$userdata['custom_user_name'] = $_POST['custom_user_name'];

// Phone
$userdata['custom_user_phone'] = $_POST['custom_user_phone'];

// Enters into DB
wp_update_user($userdata);
} ?>

Custom WordPress Registration Form

After adding the following code in your functions.php file, it will automatically add new fields “Name” and “Phone” below the username and email fields in the form. Then your registration form will look like the below image.

custom WordPress registration form

Want to add more fields simply copy a field and make some change.

If you notice carefully, you will succeed to create new and new extra fields with your WordPress registration form.

I hope you have added extra field WordPress registration form following this WordPress snippets. If this trick is helpful to add fields to the WordPress registration form please share it with your friends. Your share may be helpful for this website and your friends who need to add custom fields in Wordpress registration form.



leave a comment
Please post your comments here.