Jayesh Patel
Filed under - Development, WordPress, WordPress Plugins
No Comments so far. Add yours now

Download code here

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

  1. < ?php
  2. /*
  3. Template Name: Custom WordPress Registration
  4. */
  5. ?>

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.

  1. require_once(ABSPATH . WPINC . ’/registration.php’);
  2. global $wpdb, $user_ID;
  3. if (!$user_ID) {
  4.    //All code goes in here.
  5. }
  6. else {
  7.    wp_redirect( home_url() ); exit;
  8. }

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').

  1. <?php
  2. if(get_option(‘users_can_register’)) {
  3. //Check whether user registration is enabled by the administrator
  4. ?>
  5. <h1><?php the_title(); ?></h1>
  6. <div id=”result”></div> <!– To hold validation results –>
  7. <form action=”" method=”post”>
  8. <label>Username</label>
  9. <input type=”text” name=”username” class=”text” value=”" /><br />
  10. <label>Email address</label>
  11. <input type=”text” name=”email” class=”text” value=”" /> <br />
  12. <input type=”submit” id=”submitbtn” name=”submit” value=”SignUp” />
  13. </form>
  14. <script type=”text/javascript”>
  15. //<![CDATA[
  16. $("#submitbtn").click(function() {
  17. $('#result').html('<img src="<?php bloginfo('template_url') ?>/images/loader.gif" class="loader" />').fadeIn();
  18. var input_data = $('#wp_signup_form').serialize();
  19. $.ajax({
  20. type: "POST",
  21. url:  "",
  22. data: input_data,
  23. success: function(msg){
  24. $('.loader').remove();
  25. $('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
  26. }
  27. });
  28. return false;
  29. });
  30. //]]>
  31. </script>
  32. <?php
  33.     }
  34. else echo ”Registration is currently disabled. Please try again later.”;
  35. ?>

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.

  1. if($_POST){
  2.     //We shall SQL escape all inputs
  3.     $username = $wpdb->escape($_REQUEST['username']);
  4.     if(emptyempty($username)) {
  5.         echo ”User name should not be empty.”;
  6.         exit();
  7.     }
  8.     $email = $wpdb->escape($_REQUEST['email']);
  9.     if(!preg_match(“/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/”, $email)) {
  10.         echo ”Please enter a valid email.”;
  11.         exit();
  12.     }
  13.     $random_password = wp_generate_password( 12, false );
  14.     $status = wp_create_user( $username, $random_password, $email );
  15.     if ( is_wp_error($status) )
  16.         echo ”Username already exists. Please try another one.”;
  17.     else {
  18.         $from = get_option(‘admin_email’);
  19.         $headers = ’From: ’.$from . ”\r\n”;
  20.         $subject = ”Registration successful”;
  21.         $msg = ”Registration successful.\nYour login details\nUsername: $username\nPassword: $random_password”;
  22.         wp_mail( $email, $subject, $msg, $headers );
  23.         echo ”Please check your email for login details.”;
  24.     }
  25.     exit();
  26. }
  27.  else
  28. {
  29. //Embed the register form and javascript here
  30. }

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

  1. < ?php
  2. /*
  3. Template Name: Custom WordPress Signup Page
  4. */
  5. require_once(ABSPATH . WPINC . ’/registration.php’);
  6. global $wpdb, $user_ID;
  7. //Check whether the user is already logged in
  8. if (!$user_ID) {
  9.     if($_POST){
  10.         //We shall SQL escape all inputs
  11.         $username = $wpdb->escape($_REQUEST['username']);
  12.         if(emptyempty($username)) {
  13.             echo ”User name should not be empty.”;
  14.             exit();
  15.         }
  16.         $email = $wpdb->escape($_REQUEST['email']);
  17.         if(!preg_match(“/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/”, $email)) {
  18.             echo ”Please enter a valid email.”;
  19.             exit();
  20.         }
  21.             $random_password = wp_generate_password( 12, false );
  22.             $status = wp_create_user( $username, $random_password, $email );
  23.             if ( is_wp_error($status) )
  24.                 echo ”Username already exists. Please try another one.”;
  25.             else {
  26.                 $from = get_option(‘admin_email’);
  27.                         $headers = ’From: ’.$from . ”\r\n”;
  28.                         $subject = ”Registration successful”;
  29.                         $msg = ”Registration successful.\nYour login details\nUsername: $username\nPassword: $random_password”;
  30.                         wp_mail( $email, $subject, $msg, $headers );
  31.                 echo ”Please check your email for login details.”;
  32.             }
  33.         exit();
  34.     } else {
  35.         get_header();
  36. ?>
  37. <!– <script src=”http://code.jquery.com/jquery-1.4.4.js”></script> –>
  38. <!– Remove the comments if you are not using jQuery already in your theme –>
  39. <div id=”container”>
  40. <div id=”content”>
  41. <?php if(get_option(‘users_can_register’)) {
  42. //Check whether user registration is enabled by the administrator ?>
  43. <h1><?php the_title(); ?></h1>
  44. <div id=”result”></div> <!– To hold validation results –>
  45. <form action=”" method=”post”>
  46. <label>Username</label>
  47. <input type=”text” name=”username” class=”text” value=”" /><br />
  48. <label>Email address</label>
  49. <input type=”text” name=”email” class=”text” value=”" /> <br />
  50. <input type=”submit” id=”submitbtn” name=”submit” value=”SignUp” />
  51. </form>
  52. <script type=”text/javascript”>
  53. //<![CDATA[
  54. $("#submitbtn").click(function() {
  55. $('#result').html('<img src="<?php bloginfo('template_url') ?>/images/loader.gif" class="loader" />').fadeIn();
  56. var input_data = $('#wp_signup_form').serialize();
  57. $.ajax({
  58. type: "POST",
  59. url:  "",
  60. data: input_data,
  61. success: function(msg){
  62. $('.loader').remove();
  63. $('<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
  64. }
  65. });
  66. return false;
  67. });
  68. //]]>
  69. </script>
  70. <?php } else echo ”Registration is currently disabled. Please try again later.”; ?>
  71. </div>
  72. </div>
  73. <?php
  74.     get_footer();
  75.  } //end of if($_post)
  76. }
  77. else {
  78.     wp_redirect( home_url() ); exit;
  79. }
  80. ?>

Uploaded Files:

Leave your comment

You must be logged in to post a comment.

Share IT © 2017. All rights reserved.