Last active
August 31, 2023 19:15
-
-
Save vpadhariya/3ed2829018813036d57cb88ae7e90555 to your computer and use it in GitHub Desktop.
WordPress Auto Login Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Set a veriable for redirection (Optional) | |
$redirect_to = ''; | |
/** | |
* WordPress Auto login script | |
*/ | |
if(!is_user_logged_in()) | |
{ | |
// Take your username you want to keep for auto login. | |
$username = "abcxyz"; | |
// Check if user exist by his username | |
if($user = get_user_by('login', $username)) | |
{ | |
clean_user_cache($user->ID); | |
// Removes all of the cookies associated with authentication | |
wp_clear_auth_cookie(); | |
// Changes the current user by ID. | |
wp_set_current_user($user->ID); | |
// If secure domain then we have to set secure cookie | |
wp_set_auth_cookie($user->ID, true, (!empty($_SERVER['HTTPS']))); | |
update_user_caches($user); | |
// If user logged in then do some action. | |
if(is_user_logged_in()) | |
{ | |
// You can set redirect to create a new page | |
$redirect_to = admin_url() . 'post-new.php?post_type=page'; | |
} | |
else | |
{ | |
// Set redirect to login page | |
$redirect_to = wp_login_url(); | |
} | |
} | |
} | |
wp_safe_redirect($redirect_to); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment