Skip to content

Instantly share code, notes, and snippets.

@sagar290
Forked from cliffordp/functions.php
Created February 1, 2018 10:32
Show Gist options
  • Save sagar290/76ca46c431a556fdd5254928247292b7 to your computer and use it in GitHub Desktop.
Save sagar290/76ca46c431a556fdd5254928247292b7 to your computer and use it in GitHub Desktop.
Automatically login a single WordPress user upon arrival to a specific page. Redirect to home page once logged in. Prevent viewing the login page. Tested with WP 3.9.1. Used in functions.php
<?php
//Automatically login a single WordPress user upon arrival to a specific page.
//Redirect to home page once logged in and prevent viewing of the login page.
//Tested with WP 3.9.1. Used in functions.php
//Updated 2014-07-18 to resolve WP_DEBUG notice: "get_userdatabylogin is deprecated since version 3.3! Use get_user_by('login') instead."
//From http://tourkick.com/2014/wordpress-demo-multisite-db-reset/
function auto_login() {
//change these 2 items
$loginpageid = '1234'; //Page ID of your login page
$loginusername = 'demo'; //username of the WordPress user account to impersonate
if (!is_user_logged_in()
&& is_page($loginpageid)) { //only attempt to auto-login if at www.site.com/auto-login/ (i.e. www.site.com/?p=1234 )
//get user's ID
$user = get_user_by('login', $loginusername);
$user_id = $user->ID;
//login
wp_set_current_user($user_id, $loginusername);
wp_set_auth_cookie($user_id);
do_action('wp_login', $loginusername);
//redirect to home page after logging in (i.e. don't show content of www.site.com/?p=1234 )
wp_redirect( home_url() );
exit;
} elseif(is_page($loginpageid)) {
//prevent viewing of login page even if logged in
wp_redirect( home_url() );
exit;
} else {}
}
add_action('wp', 'auto_login');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment