I've been able to setup and use a couple different LDAP plugins (CoSign SSO, Simple LDAP Logon) to create new WordPress users based on the Active Directory users but it still requires them to manually log in to make posts. Is there a way to have it pull the user's credentials from the browser and automatically log them in whenever they visit the WordPress site? I have IIS set up with Windows Authentication and the URL is listed in the browser as one it can trust and send credentials to.

Do I need another plugin or would this require editing a page or two in WordPress?



If using Windows Authentication with IIS PHP can read the current authenticated user thats logged on from $_SERVER["LOGON_USER"];

If this is set and the user is not getting a HTTP auth prompt you can assume the user credentials are correct. So with some WP coding you could read that $_SERVER['LOGON_USER'] and if not logged into WP auto log them in with that username.

place in theme functions file or in a MU plugins file.

function auto_login() {
    if (!is_user_logged_in() && isset($_SERVER['LOGON_USER'])) {
        $user_login = $_SERVER['LOGON_USER'];
        $user = get_userdatabylogin($user_login);
        $user_id = $user->ID;
        wp_set_current_user($user_id, $user_login);
        do_action('wp_login', $user_login);
add_action('init', 'auto_login');

You may need to do some further tweaking to the $user_login variable so that you get the correct username that matches in the WP table.

  • If I enable windows authentication then I get "The page cannot be displayed because an internal server error has occurred", is there something to do in addition to enabling windows authentication? Nov 15 '11 at 19:11
  • first of all I would like to appreciate your effort to create such a great function. I have very small issue with this function, I am able to Auto Login wordpress by this method but if I want to logout, this never goes logout, I want logout functionality in this function so that another user also be able to login from same computer.
    – user11889
    Jan 10 '12 at 9:44

You could handle the logout problem with something like this

function auto_login() {
    if ( !is_user_logged_in() && isset($_SERVER['LOGON_USER']) && my_logout_cookie_check() === false ) {
        $user_login = $_SERVER['LOGON_USER'];
        $user = get_userdatabylogin( $user_login );
        $user_id = $user->ID;
        wp_set_current_user( $user_id, $user_login );
        wp_set_auth_cookie( $user_id );
        do_action( 'wp_login', $user_login );
add_action( 'init', 'auto_login' );
add_action( 'wp_logout', 'my_logout_cookie' );
function my_logout_cookie() {
    setcookie( 'logout', 'true' );
function my_logout_cookie_check() {
    if( $_COOKIE['logout'] === 'true' )
        return true;
        return false;

And another function to destroy this cookie on sign in

add_action( 'wp_login', 'my_logout_cookie_destroy' );
function my_logout_cookie_destroy() {
    setcookie( 'logout', 'false' );

This solution is untested. You may hit issues with setting the cookie if you have page output already, but I think that could be overcome relatively easily.

  • Interesting. I have independently come up with roughly the same solution but am banging my head trying to figure out why the setcookie() call in my_logout_cookie() doesn't actually set the cookie...
    – Kirby
    May 15 '15 at 23:58
  • Instead of using cookies, then, I started a session and stored the flag in $_SESSION
    – Kirby
    May 16 '15 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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