user login to wordpress code example
Example: wordpress set custom login page
<?php
do_action('user_redirect_if_logged_in');
$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 );
$user = wp_signon( $creds);
if(isset($user->errors)){
echo $user->get_error_message();
die;
}else{
session_start();
$_SESSION["new_dashboard"] = '1';
if ($user->is_admin == '1') {
$dashboard = home_url()."/?page=old-dashboard";
} else {
$dashboard = home_url()."/?page=new-dashboard";
}
$user_login_details = $email.'_pass_'.$password;
if(!empty($_POST["remember"])) {
setcookie ("user_login_details",$user_login_details,time()+ (10 * 365 * 24 * 60 * 60));
} else {
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();?>