Home > php, Php programming, wordpress > [WordPress] How to Add fields in Registration form

[WordPress] How to Add fields in Registration form

Hello Friends,
As you know the popularity of WordPress, clients are asking for customization services in WordPress website. Yesterday i was working with WordPress and I was in need to add few fields in Registration form of WordPress site without affecting WordPress Core files. This benefits when you upgrade the WordPress version, changes will remains and you will not lost the work you had done.

To add extra fields in registration form in WordPress, just place below code in functions.php file of your theme’s folder. It will automatically add new fields below username and email fields in WordPress registration form. Here magic happens…

<?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>Phone
<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);
}
?>
Advertisements
  1. September 16, 2012 at 1:40 am

    any chance there is an easy way to create a popup registration form window on wordpress.com without having a PhD in everything nerdy??? (with all much due respect)

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: