Wednesday, January 13, 2016

Wordpress logged a user programmatically

This is pretty simple and need to make sure to set the auth cookie or it will lost the user when visit through pages


 // Automatic login //   
  $username = "Admin";   
  $user = get_user_by('login', $username );   
  
// Redirect URL //   
  if ( !is_wp_error( $user ) )   
  {   
   wp_clear_auth_cookie();   
   wp_set_current_user ( $user->ID );   
   wp_set_auth_cookie ( $user->ID );   
   $redirect_to = user_admin_url();   
   wp_safe_redirect( $redirect_to );   
   exit();   
  }