1

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

2
  • 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
    Commented Jan 3, 2012 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. Commented Jan 3, 2012 at 22:48

1 Answer 1

1

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);

    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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