9. June 2011
von Blackbam
Recently I was confronted with the problem to have multiple user types on a WordPress Blog who needed to be logged in using an external service/authentication mechanism partly. But they all had to use the same login form for the system. A common problem? The WordPress Function Reference and  Codex do not document this topic very well and it took me a lot of time to figure out how to do this best, so I want to keep it for the future and provide it to my blog visitors. 

Override the WordPress default login action

 Write the following to your functions.php-file:
// this action is executed just before the invocation of the WordPress authentication process
add_action('wp_authenticate','checkTheUserAuthentication');

function checkTheUserAuthentication() {

     $username=$_POST['log'];
     $password=$_POST['pwd'];

    // try to log into the external service or database with username and password
    $ext_auth = try2AuthenticateExternalService($username,$password);

    // if external authentication was successful
    if($ext_auth) {

         // find a way to get the user id
         $user_id = username_exists($username);
         // userdata will contain all information about the user
         $userdata = get_userdata($user_id);
         $user = set_current_user($user_id,$username);

         // this will actually make the user authenticated as soon as the cookie is in the browser
         wp_set_auth_cookie($user_id);
         // the wp_login action is used by a lot of plugins, just decide if you need it
        do_action('wp_login',$userdata->ID);

        // you can redirect the authenticated user to the "logged-in-page", define('MY_PROFILE_PAGE',1); f.e. first
        header("Location:".get_page_link(MY_PROFILE_PAGE));
    }

}
  Note that this script will login your useres totally without using their passwords, so use it carefully. If you experience any problems with this way of authentication, make sure that the Cookies in the Browser are set and sent properly - make sure that setcookie() in wp-includes/pluggable.php uses all parameters of the setcookie()-function correctly. Especially check the values for COOKIE_DOMAIN, COOKIEPATH and SITECOOKIEPATH as we had and issue with that. 
Share

Dieser Eintrag wurde am 9. June 2011 um 22:22 in der Kategorie WordPress, WP Scripts veröffentlicht. You can book the comments for this article RSS 2.0. Feedback, discussion, commendation and critics are welcome: Write a comment or trackback.


Tags: , , , , , ,

Already 28 comments belonging to "WordPress custom external authentication login/logout PHP script":

Kommentare abonnieren (RSS) or URL Trackback

Cedric     says:

on 19. July 2011 at 20:35 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 20. July 2011 at 18:44 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 26. January 2012 at 03:31 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 28. January 2012 at 18:41 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 23. February 2012 at 04:00 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 26. February 2012 at 22:56 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 26. February 2012 at 23:17 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 28. January 2013 at 01:35 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 28. January 2013 at 01:55 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 26. April 2013 at 21:11 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 27. April 2013 at 11:34 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 15. May 2013 at 01:28 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 03. July 2013 at 01:21 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 03. July 2013 at 10:55 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 03. July 2013 at 20:11 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 03. July 2013 at 20:43 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 26. August 2013 at 16:51 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 29. November 2013 at 06:09 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 13. February 2014 at 21:18 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 24. February 2014 at 11:31 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 24. February 2014 at 15:02 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 26. February 2014 at 15:49 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 21. April 2014 at 07:37 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 21. April 2014 at 19:44 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 03. June 2014 at 00:44 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 25. July 2014 at 00:00 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 03. September 2014 at 19:21 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Cedric     says:

on 03. October 2014 at 17:01 on clock


Check this out: http://www.cedricve.me/blog/2011/07/18/how-to-make-use-of-wordpress-passwords/ It explains how you can make an external login with the credentials of a wordpress installation.I hope this can help you ;)

Leave a comment: