Create Login Page in WordPress

Create a custom WordPress Page



             

First of all create a custom page in the root folder of your WordPress theme and name it custom_login.php . At the top of the page write Template Name like


Using $user_login

$user_login is a global variable which helps us find the current user login data. With which we can easily find out about the following activity of a particular user.

Check if Login Failed

If user tries to login with wrong credentials, WordPress will return a query string ?login=failed. This will in turn be used to display an error message.

Check if User is already logged in

In it  I used userLoggedIn() function to check if the user is already logged in. So, if an already logged in user tends to revisit the login page, he’ll get a message about returning home or logging out. There is no sense in displaying the login form to an already logged in user.

Else display the login form

In this part I used wp_login_form() function with some arguments to display the contact form. Here you need to pay attention to the particular argument ‘redirect’ => home_url(‘/wp-admin/’), this is basically redirecting user after login to home_url(‘path-here’). You can easily change the path to redirect a user to another page E.g. a custom front-end dashboard.

Live Example

Don’t forget to publish the page from admin panel Pages > Add New > Select the template name.

        
<!– section –>   





      

   
       if(isset($_GET[‘login’])&&$_GET[‘login’]==’failed’) {            ?>        
       
     Invalid combination!
       
       
     
            } else {              
           wp_login_form($args);        
          $args = array(                     
          ‘echo’           => true,                   
          ‘redirect’       => home_url(‘/wp-admin/’),            
          ‘form_id’        => ‘loginform’,              
          ‘label_username’ => __( ‘Username’ ),                   
          ‘label_password’ => __( ‘Password’ ),                
          ‘label_remember’ => __( ‘Remember Me’ ),           
          ‘label_log_in’   => __( ‘Log In’ ),                  
          ‘id_username’    => ‘user_login’,                
          ‘id_password’    => ‘user_pass’,           
          ‘id_remember’    => ‘rememberme’,            
          ‘id_submit’      => ‘wp-submit’,            
          ‘remember’       => true,                    
          ‘value_username’ => NULL,              
          ‘value_remember’ => true                  
          );  }         ?>        
  
          <!– /section –>

0 comments: