Skip to main content

Create custom wordpress registration page


I am sure, most web masters using wordpress do not like to let their users to signup or login through the boring wordpress default signup/login screens. Everyone would like to make those pages look like same as part of their website. Today we will have a little play with creating aWordPress custom registration page template. You may not forgot my last tutorial about creating a custom wordpress login page template as part of the theme. If you hadn’t read it yet, read it hereCustom WordPress Login without using a plugin
Ok, now we will move in to our today’s tutorial.
Wordpress custom registration template

Step1: Create page custom-register.php

First we will create a new php template file called custom-register.php and place it inside your wordpress theme folder your-domain-name/wp-content/themes/your-theme-name

Step2: Naming the Template file

< ?php
/*
Template Name: Custom WordPress Registration
*/
?>

Step3: check if the user is not logged in

Like we did for custom login, we must first check whether the current user is logged in or not. We will show the registration form only if the current user is not logged in. Additionally, we need to include the file registration.php from wp-includes folder in order to create a new user inside wordpress system.
require_once(ABSPATH . WPINC . '/registration.php');
global $wpdb, $user_ID;
if (!$user_ID) {
   //All code goes in here.
}
else {
   wp_redirect( home_url() ); exit;
}

Step4: Embedding the Register Form and jQuery Ajax

Before we display the register form we need to check whether the user registration is allowed by the administrator using the function get_option('users_can_register').
<?php
if(get_option('users_can_register')) {
//Check whether user registration is enabled by the administrator
?>

<?php the_title(); ?>

<div id="result"></div> <!-- To hold validation results --> <form action="" method="post"> <label>Username</label> <input type="text" name="username" class="text" value="" /><br /> <label>Email address</label> <input type="text" name="email" class="text" value="" /> <br /> <input type="submit" id="submitbtn" name="submit" value="SignUp" /> </form> <script type="text/javascript"> //<![CDATA[ $("#submitbtn").click(function() { $('#result').html('<img src="<?php bloginfo('template_url') ?>/images/loader.gif" class="loader" />').fadeIn(); var input_data = $('#wp_signup_form').serialize(); $.ajax({ type: "POST", url: "", data: input_data, success: function(msg){ $('.loader').remove(); $('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow'); } }); return false; }); //]]> </script> <?php } else echo "Registration is currently disabled. Please try again later."; ?>

Step5: Validate the inputs and register the user

Add the following php code inside if (!$user_ID) { } and move the register form inside the else part of the following if condition.
if($_POST){
 //We shall SQL escape all inputs
 $username = $wpdb->escape($_REQUEST['username']);
 if(empty($username)) {
  echo "User name should not be empty.";
  exit();
 }
 $email = $wpdb->escape($_REQUEST['email']);
 if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
  echo "Please enter a valid email.";
  exit();
 }

 $random_password = wp_generate_password( 12, false );
 $status = wp_create_user( $username, $random_password, $email );
 if ( is_wp_error($status) )
  echo "Username already exists. Please try another one.";
 else {
  $from = get_option('admin_email');
  $headers = 'From: '.$from . "\r\n";
  $subject = "Registration successful";
  $msg = "Registration successful.\nYour login details\nUsername: $username\nPassword: $random_password";
  wp_mail( $email, $subject, $msg, $headers );
  echo "Please check your email for login details.";
 }

 exit();

}
 else
{
//Embed the register form and javascript here
}
In the above code, we will validate the inputs and then create random password with the use of function wp_generate_password(). With the use of wp_create_user function create a new user account if not the username already exists in the system. Finally, we will email the login details including the random generated password to the signed up user.

Full Code Preview

< ?php
/*
Template Name: Custom WordPress Signup Page
*/
require_once(ABSPATH . WPINC . '/registration.php');
global $wpdb, $user_ID;
//Check whether the user is already logged in
if (!$user_ID) {

 if($_POST){
  //We shall SQL escape all inputs
  $username = $wpdb->escape($_REQUEST['username']);
  if(empty($username)) {
   echo "User name should not be empty.";
   exit();
  }
  $email = $wpdb->escape($_REQUEST['email']);
  if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
   echo "Please enter a valid email.";
   exit();
  }

   $random_password = wp_generate_password( 12, false );
   $status = wp_create_user( $username, $random_password, $email );
   if ( is_wp_error($status) )
    echo "Username already exists. Please try another one.";
   else {
    $from = get_option('admin_email');
                  $headers = 'From: '.$from . "\r\n";
                  $subject = "Registration successful";
                  $msg = "Registration successful.\nYour login details\nUsername: $username\nPassword: $random_password";
                  wp_mail( $email, $subject, $msg, $headers );
    echo "Please check your email for login details.";
   }

  exit();

 } else {
  get_header();
?>
<!-- <script src="http://code.jquery.com/jquery-1.4.4.js"></script> -->
<!-- Remove the comments if you are not using jQuery already in your theme -->
<div id="container">
<div id="content">
<?php if(get_option('users_can_register')) {
//Check whether user registration is enabled by the administrator ?>

<?php the_title(); ?>

<div id="result"></div> <!-- To hold validation results --> <form action="" method="post"> <label>Username</label> <input type="text" name="username" class="text" value="" /><br /> <label>Email address</label> <input type="text" name="email" class="text" value="" /> <br /> <input type="submit" id="submitbtn" name="submit" value="SignUp" /> </form> <script type="text/javascript"> //<![CDATA[ $("#submitbtn").click(function() { $('#result').html('<img src="<?php bloginfo('template_url') ?>/images/loader.gif" class="loader" />').fadeIn(); var input_data = $('#wp_signup_form').serialize(); $.ajax({ type: "POST", url: "", data: input_data, success: function(msg){ $('.loader').remove(); $('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow'); } }); return false; }); //]]> </script> <?php } else echo "Registration is currently disabled. Please try again later."; ?> </div> </div> <?php get_footer(); } //end of if($_post) } else { wp_redirect( home_url() ); exit; } ?>
Once the template file is ready, you need to create a new page from the wordpress admin. If you are unsure about this just follow the step number 10 from my previous tutorial Custom WordPress Login without using a plugin. Hope you find this tutorial useful.
1,658 Downloads

Comments

Popular posts from this blog

உடல் எடையை குறைக்க வேண்டுமா ?

இன்றைய அவசர உலகின் மிக பெரிய பிரச்சனையாக இருப்பது உடல் எடை அதிகரிப்பது தான்.மனம் போன போக்கில் உணவு கட்டு பாடு இல்லாமல் கண்டதையும் உள்ளே தள்ளுவதும்,உக்காந்த இடத்திலேயே கணணி முன் நேரத்தை விரயமாக்குவதும் தான் இந்த பிரச்சனைக்கு மூல காரணமாகும். அது சரி இந்த பிரச்சனையை எப்படி இல்லாமல் செய்வது அல்லது உடல் எடையை எவ்வாறு குறைப்பது என்பதை பற்றி பாப்போம் , பல வருட ஆரய்சிக்குபின் மருத்துவர்கள்   உடல் எடைய குறைக்க மிகவும் சுலபமான உடற்பயிற்சியை கண்டுபிடித்துள்ளனர்.இது  100% பயனளிக்க கூடியது, எந்த இடத்திலும் எந்தநேரத்திலும் மிக சுலபமா செய்ய கூடிய உடற் பயிற்சியாகும்.இந்த உடற்பயிற்சிகள் படத்துடன் கீழே தரப்பட்டுள்ளது நீங்களும் முயற்சித்து பாருங்கள கண்டிப்பாக பலன் கிடைக்கும்... முதலில் நாற்காலியில் உட்கார்ந்து இட  பக்கம் பார்கவும் .. ..        அடுத்து  நாற்காலியில் உட்கார்ந்து வல  பக்கம் பார்கவும்  ....  நண்பர்கள் யாரவது மச்சி வாடா சின்ன பீஸ் ,இங்க பாரு சூப்பர் அய்டம்னு சொல்லி கால்ல விழுந்து கூப்பிட்டலோ மேற்கூறிய உடற் பயி...

38 (new) web tools to keep you busy

For many of us, the internet represents our daily job, income resource or biggest hobby. Every day we check our emails, read our feeds, visit our websites, find and discuss new things and GOD knows what else. It requires a lot of tools to do all this stuff and sometimes, we forget to search for easier solutions losing valuable time or keeping down the production graph. It's very hard to keep track with everything that's new and popular and this is why we do monthly searches for the best web tools out there. Enjoy!  45+ Web Operating Systems "There are many more web operating systems hoping to bring all your usual desktop applications online in one place - here are more than 45 of our favorites."  15 Ways To Create Website Screenshots "15 Ways To Create Website Screenshots"  Open Source Windows "The promise of open source software is best quality, flexibility and reliability. The only way to have TRUE "Open Source Windows"is...

(Loot again) Get Mi Earphones & Mi Backpack In Just Rs.1

How to Get Mi Band , Mi Earphones & Mi Backpack In Just Rs.1 1, First Of All Just  Click On Below Links 1 By 1  Click 1->  Mi City Backpack Dark Grey Minimalist sleek design  -In Just Rs.1 Click 2->  Mi Earphones Silver Dynamic bass sound  -In Just Rs.1 2. Now Open All Links 1 By 1 3. After Opening The Link Click On “ Click to Bring The Price Down ” Then Click “ I Want it too ” 4. Login into Your Mi Account ( Or Sign Up  –  New Mi Account Giving Huge Price drop Like Rs.50 Or Rs.70 Drop , So Recommend You to Make New Account ) 5. Now You will See 3 Products There In this  Mi 24 Hour Madness Sale  6. Click On “ Participate ” Now Button & Share The Link With Your Friends When Each Friend Click On Your Link , You Will See Some Price cut in Your Products… Finally After Enough Click Your Product Will be Available For Just Rs.1  7. Start Referring , Start Looting MI