Create custom registration form wordpress

Rakesh Sharma      1 Comment  

Create custom registration form wordpress

You can easily customize inbuilt forms of wordpress. but sometimes need own custom register form to show or use many extra fields. there is also many plugins for do this. but i am sharing a simple way to create custom registration form in wordpress. i am using whole custom code in wordpress way to register a user with own custom register form. we need to follow some steps and we done with custom registration form.

1. create a file “wp-content/themes/your_theme/custom-register.php” and add below code

<?php
/**
 Template Name: Register
 */

get_header(); ?>

<div id="main-content" class="main-content">
  <div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">
      <?php if (!is_user_logged_in()) {?>
      <div class="container" style="margin:100px;">
        <div class="step1">
          <div class="row-fluid">
            <div class="span12">
              <h3> Greetings: Create an account</h3>
            </div>
          </div>
          <div>
            <?php if(defined('REGISTRATION_ERROR')){
		    foreach(unserialize(REGISTRATION_ERROR) as $error){
		      echo '<p class="order_error">'.$error.'</p><br>';
		    }
		  }?>
          </div>
        </div>
        <div class="row-fluid">
          <div class="span12">
            <form id="my-registration-form" method="post" action="<?php echo add_query_arg('do', 'register', get_permalink( $post->ID )); ?>" class="form_comment">
              <div class="span6">
                <input value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" name="email" id="email" placeholder="Email"  required="" type="text">
                <span class="toolTip" title="Enter a valid email address. your order will delivered on this email box">&nbsp;</span>
                <input value="" name="pass" id="password" placeholder="Password"  required="" type="Password">
                <span class="toolTip" title="Use atleast 6 characters">&nbsp;</span>
                <input value="" name="cpass" id="cpassword" placeholder="Confirm Password"  required="" type="Password">
                <span class="toolTip" title="Confirm your password">&nbsp;</span> </div>
              <div class="span6">
                <input value="<?php if(isset($_POST['user'])) echo $_POST['user'];?>" name="user" id="username" placeholder="User Name"  required="" type="text">
                <span class="toolTip" title="Enter your username">&nbsp;</span>
                <input value="<?php if(isset($_POST['phone'])) echo $_POST['phone'];?>" name="phone" id="phone" placeholder="Phone"  required="" type="text">
                <div>
                  <input name="submit" type="submit" class="btn-ser" value="Sign Up & Continue" style="width:160px !important; padding-left:18px;">
                </div>
              </div>
            </form>
          </div>
        </div>
      </div>
      <?php } ?>
    </div>
    <!-- #content -->
  </div>
  <!-- #primary -->
</div>
<!-- #main-content -->
<?php
get_sidebar();
get_footer();

2. Go to admin create a New page with any name and choose Template “Register”.

register

3. Add below code “wp-content/themes/your_theme/functions.php”

/**
 * Registering a new user.
 */
add_action('template_redirect', 'register_user');

function register_user(){
  if(isset($_GET['do']) && $_GET['do'] == 'register'):
    $errors = array();
    if(empty($_POST['user'])) 
	   $errors[] = 'Please enter a fullname.<br>';
	if(empty($_POST['email'])) 
	   $errors[] = 'Please enter a email.<br>';
	if(empty($_POST['pass'])) 
	   $errors[] = 'Please enter a password.<br>';
	if(empty($_POST['cpass'])) 
	   $errors[] = 'Please enter a confirm password.<br>';
	if((!empty($_POST['cpass']) && !empty($_POST['pass'])) && ($_POST['pass'] != $_POST['cpass'])) 
	   $errors[] = 'Entered password did not match.';
    $user_login = esc_attr($_POST['user']);
    $user_email = esc_attr($_POST['email']);
	$user_pass = esc_attr($_POST['pass']);
	$user_confirm_pass = esc_attr($_POST['cpass']);
	$user_phone = esc_attr($_POST['phone']);
	$sanitized_user_login = sanitize_user($user_login);
    $user_email = apply_filters('user_registration_email', $user_email);
 
    if(!is_email($user_email)) 
	   $errors[] = 'Invalid e-mail.<br>';
    elseif(email_exists($user_email)) 
	   $errors[] = 'This email is already registered.<br>';
 
    if(empty($sanitized_user_login) || !validate_username($user_login)) 
	   $errors[] = 'Invalid user name.<br>';
    elseif(username_exists($sanitized_user_login)) 
	   $errors[] = 'User name already exists.<br>';
 
    if(empty($errors)):
      $user_id = wp_create_user($sanitized_user_login, $user_pass, $user_email);
 
    if(!$user_id):
      $errors[] = 'Registration failed';
    else:
	  update_user_option($user_id, 'default_password_nag', true, true);
	  wp_new_user_notification($user_id, $user_pass);
	  update_user_meta ($user_id, 'user_phone', $user_phone);
	  wp_cache_delete ($user_id, 'users');
      wp_cache_delete ($user_login, 'userlogins');
      do_action ('user_register', $user_id);
	  $user_data = get_userdata ($user_id);
      if ($user_data !== false) {
	     wp_clear_auth_cookie();
         wp_set_auth_cookie ($user_data->ID, true);
         do_action ('wp_login', $user_data->user_login, $user_data);
		 // Redirect user.
         wp_redirect ('?page_id=213');
         exit();
       }
      endif;
    endif;
 
    if(!empty($errors)) 
	  define('REGISTRATION_ERROR', serialize($errors));
  endif;
}

form-register

Now you are all done with creating custom registration form in worpress.

  • Adarsh

    Thanks