Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a simple auto log in hook that looks like the following:

function auto_login() {
    if (!is_user_logged_in()) {
        //Removed some code for brevity.

        $user = get_userdatabylogin($domainName);

        if ($user != null) {
            //Set the auth cookie.
            wp_set_auth_cookie($user->ID, false, null);
            //Set the current user (this will also set WP objects at the global level)
            wp_set_current_user($user->ID);
        }
    }
}
add_action('init', 'auto_login');

This code does work, but, in order to get the admin bar at the top of the page it requires a page refresh. This makes me think that I'm not getting some global objects populated early enough in the Wordpress page life cycle.

Should I be calling this method somewhere else besides init?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Maybe it is too late to hook on init. Try set_current_user or some earlier hook. List is here: https://codex.wordpress.org/Plugin_API/Action_Reference

share|improve this answer
    
Cheers. It looks like set_current_user is early enough in the cycle to accomplish this. –  Ek0nomik Feb 1 '13 at 14:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.