wordpress generate login form code example

Example: wordpress set custom login page

<?php
   
   /* Template Name: login-new */ 
   do_action('user_redirect_if_logged_in');

   // get_header();
   $login = home_url()."/login-new/";
   $dashboard = home_url()."/dashboard/";
   
   if(isset($_REQUEST['signin'])){
      $email = $_POST['email'];
      $password = $_POST['password'];
      $creds = array();
      $creds['user_login'] = $_POST['email'];
      $creds['user_password'] = $_POST['password'];
      $creds['remember'] = false;
      $user = wp_signon( $creds, false );
      //if ( is_wp_error($user) )
      //{
      //   header("location:$login");
      //}
      //else
      //{
      //   header("location:$dashboard");
      //} 

      $user = wp_signon( $creds);

      if(isset($user->errors)){
      // if(is_wp_error($user)) {
         echo $user->get_error_message();
         die;
      }else{ //successfully logged in
            session_start();  //check for wp_session storage 
            $_SESSION["new_dashboard"] = '1';  //if you want to redirect user to a new page or set any conditions on login
         
                if ($user->is_admin == '1') {
                  $dashboard = home_url()."/?page=old-dashboard";
                } else {
                  $dashboard = home_url()."/?page=new-dashboard";
                }
        
        //set cookie for remember me //save user login details as cookie if remember me is set, so that if user logs out next time and comes to this log in page, username & password auto fills by checking
        $user_login_details = $email.'_pass_'.$password;
        if(!empty($_POST["remember"])) {
          setcookie ("user_login_details",$user_login_details,time()+ (10 * 365 * 24 * 60 * 60)); //set cookie time as per you need
        } else {  //remove login details from cookie
          if(isset($_COOKIE["user_login_details"])) {
            setcookie ("user_login_details","");
          }
        }
         wp_redirect($dashboard);
         exit;
      }
   }
   
   if(isset($_COOKIE["user_login_details"])) {
          $login_details = $_COOKIE["user_login_details"];
          $login_details = explode('_pass_', $login_details);
          $email_set = $login_details[0];
          $pass_set = $login_details[1];
   }

   ?>

<!DOCTYPE html>
<html lang="en">

<head>
<?php 
   global $wpdb;
   $db_name = $wpdb->dbname;
?>

   <title>Sign In</title>

</head>

<title>New Login</title>

<div class="container-fluid">
   <div class="row">
      <div class="col-md-4 card-body">
     
         <div class="card">
            <div class="card-body">
             
               <h3 class="h3">Login</h3>

               <form method="POST" action="">                  
                  <div class="form-group">
                     <label>Email address</label>
                     <div class="input-group">
                        <input id="email" type="email" class="form-control" name="email" value="<?php if(isset($email_set)){ echo $email_set; } ?>" required />
                     </div>
                  </div>
                  <div class="form-group">
                  
                           <label class="form-control-label">Password</label>
                    
                           <a href="<?php echo site_url(); ?>/password-reset/" class="small text-muted text-underline--dashed border-primary">Lost password?</a>
                  
                     <div class="input-group input-group-merge">
             
                        <input id="password" type="password" value="<?php if(isset($pass_set)){ echo $pass_set; } ?>" class="form-control" name="password" required />
                        
                           <a href="#/" onClick="show_pass()" class="primary_color">
                           <i  class="fas fa-eye"></i>
                           </a>
                     </div>
                   
                           <input type="checkbox" name="remember" class="custom-control-input" id="remember" >
                           <label class="custom-control-label" for="remember">Remember Me</label>
                      
                        <button type="submit" id="signin" name="signin">
                        
                        </button>
                
                  </div>
               </form>
               
            </div>
         </div>
      </div>
   </div>
</div>
<script>
function show_pass() {
  var x = document.getElementById("password");
  if (x.type === "password") {
    x.type = "text";
  } else {
    x.type = "password";
  }
}
</script>

<?php get_footer();?>

Tags:

Php Example