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'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

share|improve this question
    
have you tried putting it in your functions.php file? you would need to enclose it in an if statement since you don't want to run that code when the user is already logged in. –  rexposadas Jan 3 '12 at 22:06
    
Are you talking about the theme's plugin.php? I'd prefer to keep the code in a plugin since we will have multiple themes. Regarding your second statement, that was a snippet that I pasted above. The actual code in inside a class and an if statement that checks is_user_logged_in to make sure that the user is not already logged in. –  weberwithoneb Jan 3 '12 at 22:48
add comment

1 Answer

up vote 1 down vote accepted

I ended up using the 'plugins_loaded' action or state from http://codex.wordpress.org/Plugin_API/Action_Reference:

In my main plugin file I have:

include_once( 'lib/class-my-auth.php' ); // your class file here
add_action( 'plugins_loaded', 'My_Auth::auto_login' );

In lib/class-my-auth.php:

<?php

class My_Auth {

    private static $successfully_connected_Main_to_WP = false;

    public static function auto_login() {

            $username = ...; // Integrate with main site to get username from active session

            // Check if WP user is logged in
            if ( is_user_logged_in() ) {

                // Get current WP user
                $current_wp_user = wp_get_current_user();

                // Logout if the current WP user is different than the main site user
                if ( strToLower( $username ) !== strToLower( $curren_wp_user->user_login ) ) {
                    self::logout_of_wp();
                } else {
                    self::$successfully_connected_Main_to_WP = true;
                }

            }

            // If a connection b/w main site & WP has not been established, login if possible
            if ( ! self::$successfully_connected_Main_to_WP && $user_info = get_userdatabylogin($username) ) {

                $user_id = $user_info->ID;

                if ( $user_id > 0 ) {
                    wp_set_auth_cookie( $user_id );
                    wp_set_current_user( $user_id );
                    self::$successfully_connected_Main_to_WP = true;
                }

            }

        }

        // If no connection b/w main site & WP was established, and the user is
        // logged in, logout.
        if ( ! self::$successfully_connected_Main_to_WP && is_user_logged_in() ) {
            self::logout_of_wp();
        }

    }

    private static function logout_of_wp() {

        // Clear the auth cookie, and do other stuff
        wp_clear_auth_cookie();
        do_action('wp_logout');

        // Unset the current user
        wp_set_current_user(0);

    }

}
share|improve this answer
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.