I'm using this (simplified) code to automatically login users via a plugin for a single sign-on system:
$user_info = get_userdatabylogin( $username );
$user_id = $user_info->ID;
wp_set_current_user( $user_id );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $username );
Many code snippets that I've found online will bind this code to the init
action. When using init
, I'm having some trouble with the timing of elements loading on the page. For example, the user toolbar only appears on the second page load while the Log In
meta link changes to Logout
on the first. It seems that some elements are loading before the user session is setup.
When should I load this code? Looking at http://codex.wordpress.org/Plugin_API/Action_Reference, is plugins_loaded the best time?
Thanks, Mike