I am testing to login user via a PHP API, I hardcoded the parameters for the moment:

    $creds = array();
        $creds['user_login'] = 'foo';
        $creds['user_password'] = 'bar';

    $user = wp_signon( $creds, false );
    if ( is_wp_error($user) )
        echo $user->get_error_message();
        echo 'login successfull';

The problem is that it's testing the integrity of username but not the password, eventhough I am pretty sure the password is wrong, the login is successfull which means the signon function is not working as it's supposed to. I am including the wp-config file at the top of file and I tested the wp DB connection before posting:

require_once ('../wp-config.php');

Am I missing anything ?


I am debugging the $user variable: var_dump($user); and here is what I am getting:

    login successfull object(WP_User)#304 (7) { ["data"]=> object(stdClass)#305
 (10) { ["ID"]=> string(2) "75" ["user_login"]=> string(5) "foo" ["user_pass"]=> 
string(34) "$P$B5uc93mdbK3R8QDOa0uC/fDnUu8rkP1" ["user_nicename"]=> string(5) 
"foo" ["user_email"]=> string(23) "foo.user@gmail.com" ["user_url"]=> string(0) ""
 ["user_registered"]=> string(19) "2014-05-15 17:52:45" ["user_activation_key"]=> 
string(0) "" ["user_status"]=> string(1) "0" ["display_name"]=> string(5) "foo" }
 ["ID"]=> int(75) ["caps"]=> array(1) { ["subscriber"]=> bool(true) } 
["cap_key"]=> string(15) "wp_capabilities" ["roles"]=> array(1) { [0]=> string(10)
 "subscriber" } ["allcaps"]=> array(3) { ["read"]=> bool(true) ["level_0"]=> 
bool(true) ["subscriber"]=> bool(true) } ["filter"]=> NULL }

Whenever I mistype the username, an error is thrown which is pretty obvious:

ERROR: Invalid username. Lost your password?ERROR: Invalid username. Lost your password?

But password has always passed the test eventhough I am sure it's wrongly typed.

  • What do you get with var_dump($user);?
    – s_ha_dum
    May 18 '14 at 22:30
  • Oh sorry, I am getting a full stack, I am updating my question accordingly
    – Malloc
    May 18 '14 at 22:32
  • You got that string? Interesting. I'll have to dig though the source.
    – s_ha_dum
    May 18 '14 at 22:34
  • Hi, I edited my post, please have a look
    – Malloc
    May 18 '14 at 22:36
  • Change the password to make 100% sure it is wrong. And are there any plugins that might effect logins?
    – s_ha_dum
    May 18 '14 at 22:39

The wp_signon function is probably working exactly as it is supposed to. What you're missing is that that other methods of authentication exist, like the browser cookies, and wp_signon will check those too. Is your browser sending your WordPress authentication cookies to your script?

The bottom line is that you should not be using wp_signon here, or realistically, anywhere. See, the wp_signon function performs the entire authentication stack, not just a simple username and password check. People can authenticate in ways other than providing a username and password, and wp_signon will check them all, including methods added by plugins and anything else that hooks to the authenticate filter.

The wp_signon function is intended for actually authenticating to WordPress itself, not to your script. You're using the wrong function for your purpose.

If you simply want to check a username and password, use the wp_authenticate_username_password function instead.

Basically, the entire notion of "signing in a user" doesn't actually make any sense from a PHP script standpoint. You can verify a user's credentials and then simply do wp_set_current_user to become that user, but the code can really be any user it wants to be. You can call wp_set_current_user to "become" any user, without any credentials at all. A custom PHP script has total access to start with.

So, really, there's never any need to call wp_signon unless you're wanting to a) authenticate and then b) send back authentication cookies to the users browser. If you're not actively talking to a user's browser to authenticate that browser to WordPress via cookies, then you don't need to be doing wp_signon, ever.

If you need to verify a user's credentials from somewhere else, then there are many functions to do that. You can verify those credentials using whatever means you like and then wp_set_current_user to become that user for this particular session only. If you want to log them in on a more permanent basis, that is possible too by sending them authentication cookies, but you'd probably want to do that more directly, not through wp_signon. And even then, if that's what you're doing, then a far simpler approach would be to simply create a new authentication function that returns a valid WP_User based on your check, and hook it to the authenticate filter. Then your custom code for authentication becomes much simpler indeed.

So, define what you're doing. Then people can give you the best way to do it. That best way will never really involve calling wp_signon yourself, ever. :)

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.