Create Custom Login And Register Without Using Plugin In WordPress

Today, you will learn how to create a custom login and register without a plugin in WordPress. There are so many different ways of customizing the WordPress login page and register page process. There are two functions that are very important for logging in and registering. The first method is to check the username and the password. If your username and password are wrong, then this method will show an error in your Browser;  and the second method is to create new user information and save it into your WordPess database. 

WordPress Function

  1. wp_signon – It helps to authenticate a user with the option to remember the credentials.
  2. wp_create_user – This function allows you to insert a new user into the WordPress database.
Example: Wordpress Two Method parameters.
 
wp_signon
  1. $login_data = array();  
  2.  $login_data['user_login'] = $username;  
  3.  $login_data['user_password'] = $password;  
  4.  $login_data['remember'] = $remember;  
  5.  $user_verify = wp_signon( $login_data, false );  
wp_create_user
  1. <?php  
  2.   
  3. // Check username is present and not already in use  
  4.   
  5. $username = $wpdb->escape($_REQUEST['username']);  
  6.   
  7. if (strpos($username' ') !== false)  
  8.     {  
  9.     $errors['username'] = "Sorry, no spaces allowed in usernames";  
  10.     }  
  11.   
  12. if (emptyempty($username))  
  13.     {  
  14.     $errors['username'] = "Please enter a username";  
  15.     }  
  16. elseif (username_exists($username))  
  17.     {  
  18.     $errors['username'] = "Username already exists, please try another";  
  19.     }  
  20.   
  21. // Check email address is present and valid  
  22.   
  23. $email = $wpdb->escape($_REQUEST['email']);  
  24.   
  25. if (!is_email($email))  
  26.     {  
  27.     $errors['email'] = "Please enter a valid email";  
  28.     }  
  29. elseif (email_exists($email))  
  30.     {  
  31.     $errors['email'] = "This email address is already in use";  
  32.     }  
  33.   
  34. // Check password is valid  
  35.   
  36. if (0 === preg_match("/.{6,}/"$_POST['password']))  
  37.     {  
  38.     $errors['password'] = "Password must be at least six characters";  
  39.     }  
  40.   
  41. // Check password confirmation_matches  
  42.   
  43. if (0 !== strcmp($_POST['password'], $_POST['password_confirmation']))  
  44.     {  
  45.     $errors['password_confirmation'] = "Passwords do not match";  
  46.     }  
  47.   
  48. // Check terms of service is agreed to  
  49.   
  50. if ($_POST['terms'] != "Yes")  
  51.     {  
  52.     $errors['terms'] = "You must agree to Terms of Service";  
  53.     }  
  54.   
  55. if (0 === count($errors))  
  56.     {  
  57.     $password = $_POST['password'];  
  58.     $new_user_id = wp_create_user($username$password$email);  
  59.   
  60.     // You could do all manner of other things here like send an email to the user, etc. I leave that to you.  
  61.   
  62.     $success = 1;  
  63.     header('Location:' . get_bloginfo('url') . '/login/?success=1&u=' . $username);  
  64.     }  

Let's start with the following steps.

Step 1: To create a template in WordrPess, Create PHP file like new –login.php.

Go to your active theme.

Theme

Open the active theme folder and save the PHP file which is created.

theme

Open PHP file into PHP editor like Netbeans , Notepad++ etc.

Step 2: Open the file and write the template name, shown in the example, given below:

Open
The changes required are:
 
html

Save it.

Step 3: Go to WordPress admin panel and create WordPress pages.

Click Pages-> Add New.

New

Click publish.

Step 4: Open your pages in the Browser. Show login HTML in your pages.

login

Step 5: If you enter the wrong information, then it will display the error as follows:

error

On entering the correct information, the following screenshot will emerge:
information

Login Code display
 

  1. <?php  
  2. /** 
  3.  * Template Name: login page 
  4.  */  
  5.  get_header();   
  6.    
  7.    
  8. if($_POST
  9. {  
  10.    
  11.     global $wpdb;  
  12.    
  13.     //We shall SQL escape all inputs  
  14.     $username = $wpdb->escape($_REQUEST['username']);  
  15.     $password = $wpdb->escape($_REQUEST['password']);  
  16.     $remember = $wpdb->escape($_REQUEST['rememberme']);  
  17.    
  18.     if($remember$remember = "true";  
  19.     else $remember = "false";  
  20.    
  21.     $login_data = array();  
  22.     $login_data['user_login'] = $username;  
  23.     $login_data['user_password'] = $password;  
  24.     $login_data['remember'] = $remember;  
  25.    
  26.     $user_verify = wp_signon( $login_data, false );   
  27.    
  28.     if ( is_wp_error($user_verify) )   
  29.     {  
  30.         echo "Invalid login details";  
  31.        // Note, I have created a page called "Error" that is a child of the login page to handle errors. This can be anything, but it seemed a good way to me to handle errors.  
  32.      } else
  33.     {    
  34.        echo "<script type='text/javascript'>window.location.href='". home_url() ."'</script>";  
  35.        exit();  
  36.      }  
  37.    
  38. else 
  39. {  
  40.    
  41.     // No login details entered - you should probably add some more user feedback here, but this does the bare minimum  
  42.    
  43.     //echo "Invalid login details";  
  44.    
  45. }  
  46.  ?>  
  47.   
  48. <form id="login1" name="form" action="<?php echo home_url(); ?>/login/" method="post">  
  49.   
  50.         <input id="username" type="text" placeholder="Username" name="username"><br>  
  51.         <input id="password" type="password" placeholder="Password" name="password">  
  52.         <input id="submit" type="submit" name="submit" value="Submit">  
  53. </form>    
  54. <?php get_footer(); ?>  
The processing is shown below:

register
 
register

Fill in information

information

Click sign up button.

Go to the database and show the record.

record

Register file code
  1. <?php  
  2. /* 
  3. Template Name: Register 
  4. */  
  5.    
  6. get_header();   
  7. global $wpdb$user_ID;  
  8. //Check whether the user is already logged in  
  9. if ($user_ID
  10. {  
  11.    
  12.     // They're already logged in, so we bounce them back to the homepage.  
  13.    
  14.     header( 'Location:' . home_url() );  
  15.    
  16. else
  17.  {  
  18.    
  19.     $errors = array();  
  20.    
  21.     if$_SERVER['REQUEST_METHOD'] == 'POST' ) 
  22.       {  
  23.    
  24.         // Check username is present and not already in use  
  25.         $username = $wpdb->escape($_REQUEST['username']);  
  26.         if ( strpos($username' ') !== false )
  27.         {   
  28.             $errors['username'] = "Sorry, no spaces allowed in usernames";  
  29.         }  
  30.         if(emptyempty($username)) 
  31.         {   
  32.             $errors['username'] = "Please enter a username";  
  33.         } elseif( username_exists( $username ) ) 
  34.         {  
  35.             $errors['username'] = "Username already exists, please try another";  
  36.         }  
  37.    
  38.         // Check email address is present and valid  
  39.         $email = $wpdb->escape($_REQUEST['email']);  
  40.         if( !is_email( $email ) ) 
  41.         {   
  42.             $errors['email'] = "Please enter a valid email";  
  43.         } elseif( email_exists( $email ) ) 
  44.         {  
  45.             $errors['email'] = "This email address is already in use";  
  46.         }  
  47.    
  48.         // Check password is valid  
  49.         if(0 === preg_match("/.{6,}/"$_POST['password']))
  50.         {  
  51.           $errors['password'] = "Password must be at least six characters";  
  52.         }  
  53.    
  54.         // Check password confirmation_matches  
  55.         if(0 !== strcmp($_POST['password'], $_POST['password_confirmation']))
  56.          {  
  57.           $errors['password_confirmation'] = "Passwords do not match";  
  58.         }  
  59.    
  60.         // Check terms of service is agreed to  
  61.         if($_POST['terms'] != "Yes")
  62.         {  
  63.             $errors['terms'] = "You must agree to Terms of Service";  
  64.         }  
  65.    
  66.         if(0 === count($errors)) 
  67.          {  
  68.    
  69.             $password = $_POST['password'];  
  70.    
  71.             $new_user_id = wp_create_user( $username$password$email );  
  72.    
  73.             // You could do all manner of other things here like send an email to the user, etc. I leave that to you.  
  74.    
  75.             $success = 1;  
  76.    
  77.             //header( 'Location:' . get_bloginfo('url') . '/login/?success=1&u=' . $username );  
  78.    
  79.         }  
  80.    
  81.     }  
  82. }  
  83.   
  84. ?>  
  85.   
  86. <form id="wp_signup_form" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post">  
  87.   
  88.         <label for="username">Username</label>  
  89.         <input type="text" name="username" id="username">  
  90.         <label for="email">Email address</label>  
  91.         <input type="text" name="email" id="email">  
  92.         <label for="password">Password</label>  
  93.         <input type="password" name="password" id="password">  
  94.         <label for="password_confirmation">Confirm Password</label>  
  95.         <input type="password" name="password_confirmation" id="password_confirmation">  
  96.   
  97.         <input name="terms" id="terms" type="checkbox" value="Yes">  
  98.         <label for="terms">I agree to the Terms of Service</label>  
  99.   
  100.         <input type="submit" id="submitbtn" name="submit" value="Sign Up" />  
  101.   
  102. </form>  
  103.   
  104. <?php get_footer(); ?>

Up Next
    Ebook Download
    View all
    Learn
    View all