Ganesh Veer

Ganesh Veer

Alandi Dewachi, Pune

How to create custom registration form on your wordpress website?

April 10, 2014, by admin, category Wordpress

1. Adding a new form elements – Here I have added three new form elements to the registration form first name , last name and association bar number

   add_action('register_form','myplugin_register_form');
    function myplugin_register_form (){
        $first_name = ( isset( $_POST['first_name'] ) ) ? $_POST['first_name']: '';
        $last_name = ( isset( $_POST['last_name'] ) ) ? $_POST['last_name']: '';
        $abar_no = ( isset( $_POST['abar_no'] ) ) ? $_POST['abar_no']: '';
        ?>
        <p>  <label for="first_name"><?php _e('First Name','mydomain') ?><br />
                <input type="text" name="first_name" id="first_name" class="input" value="" size="25" />
          </p>
        <p> <label for="last_name"><?php _e('Last Name','mydomain') ?><br />
                <input type="text" name="last_name" id="last_name" class="input" value="" size="25" />
        </p>
      <p> <label for="abar_no"><?php _e('Abar No','mydomain') ?><br />
                <input type="text" name="abar_no" id="abar_no" class="input" value="" size="25" />
        </p>
        <?php    }

2. Add validation– In this case, we make sure first_name, last_name and abar_no is required.

    add_filter('registration_errors', 'myplugin_registration_errors', 10, 3);
    function myplugin_registration_errors ($errors, $sanitized_user_login, $user_email) {

        if ( empty( $_POST['first_name'] ) )
            $errors->add( 'first_name_error', __('ERROR: You must include a first name.','mydomain') );

		if ( empty( $_POST['last_name'] ) )
            $errors->add( 'last_name_error', __('ERROR: You must include a last name.','mydomain') );

	if ( empty( $_POST['abar_no'] ) )
            $errors->add( 'abar_no_error', __('ERROR: You must include a association bar_no.','mydomain') );

        return $errors;
    }

3. Finally, save our new custom fields, registration user meta.

    add_action('user_register', 'myplugin_user_register');
    function myplugin_user_register ($user_id) {
        if ( isset( $_POST['first_name'] ) )
            update_user_meta($user_id, 'first_name', $_POST['first_name']);

		if ( isset( $_POST['last_name'] ) )
            update_user_meta($user_id, 'last_name', $_POST['last_name']);

		if ( isset( $_POST['abar_no'] ) )
            update_user_meta($user_id, 'abar_no', $_POST['abar_no']);

    }

Display Custom registration field on back end, profile edit page

	function add_contact_fields($profile_fields) {
	// Adding fields
	$profile_fields['abar_no'] = 'Association Bar Number';
	return $profile_fields;
}
// Adding the filter
add_filter('user_contactmethods', 'add_contact_fields');

Comments are closed.