wordpress branded login code example
Example 1: 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();?>
Example 2: wordpress set two login pages
<?php
//if you want to show two (different) login pages for two (different) links,
//you can first of all think of two urls for two different login pages.
//here, i have two links => /login-old and /login-new for which i want different forms,
//now as any of these url hits the browser, i will check for url paramenter
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'login-new') !== false) {
// echo 'to New Login page';
$cookie_name = "login_page";
$cookie_value = "new";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); //will not set immediately, but useful later for logout
header("Location: ".site_url('login')); //this will be your default login page url
exit;
} else if(strpos($url,'login-old') !== false) {
// echo 'to Old Login page';
$cookie_name = "login_page";
$cookie_value = "old";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); //will not set immediately, but useful later for logout
header("Location: ".site_url('login'));
exit;
}
//now that the cookie is set, i know which form to show to user,
//now, as the user gets redirected to default login page, go to it's template file
//and check for the default login form, where, check,
$login_page = '';
if (isset($_COOKIE['login_page'])) {
$login_page = $_COOKIE['login_page'];
}
if ($login_page == 'new') { ?>
<style>
#your new form styling here...
</style>
<?php } else if ($login_page == 'old'){ ?>
<style>
#your old form styling here...
</style>
<?php }
if ($login_page == 'new') { ?>
<form id="new_form" action="" method="post"> </form>
<?php } else if ($login_page == 'old'){ ?>
<form id="old_form" action="" method="post"> </form>
<?php }
//here, check the default login form action attr to put above in our custom forms