wordpress login code example
Example 1: wordpress register user php
<php
$user = (isset($_POST['username']) ? $_POST['username'] : '');
$pass = (isset($_POST['password']) ? $_POST['password'] : '');
$email = (isset($_POST['email']) ? $_POST['email'] : '');
if (!username_exists($user) && !email_exists($email)) {
$user_id = wp_create_user($user, $pass, $email);
if (!is_wp_error($user_id)) {
$user = new WP_User($user_id);
$user->set_role('do');
wp_redirect('URL_where_you_want_redirect');
exit;
} else {
}
}else{
}
?>
Example 2: last login date time in wordpress
function user_last_login( $user_login, $user ) {
update_user_meta( $user->ID, 'last_login', time() );
}
add_action( 'wp_login', 'user_last_login', 10, 2 );
echo get_user_meta( $user->ID, 'last_login' , true );
Example 3: wordpress admin url
<?php
echo '<a href="' . esc_url( admin_url('nav-menus.php') ) . '">' . esc_html( 'Add Menu' ) . '</a>';
?>
Example 4: 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();?>
Example 5: wordpress login user programmatically
<?php
function auto_login( $user ) {
$username = $user;
if ( !is_user_logged_in() ) {
$user = get_userdatabylogin( $username );
$user_id = $user->ID;
wp_set_current_user( $user_id, $user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user_login );
}
}
?>
Example 6: wordpress login programmatically
<?php
clean_user_cache($user_id);
wp_clear_auth_cookie();
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id, true, false);
$user = get_user_by('id', $user_id);
update_user_caches($user);
?>