0

I am working on a little script that requires the user role right after logging in. I Figured wp_login() would be perfect. I read this runs after the login of a user and contains 2 parameter of which one is the username. With the username I can figure out their role. But it seems wp_login() has been deprecated for quite a while now. I don't think wp_signon() can fill my needs in the same way wp_login() can. The question remains, should I use the wp_login() hook?

3

No, you shouldn't, it's deprecated

I don't think wp_signon() can fill my needs in the same way wp_login() can

It does the exact same thing, but with a few new features

I read this runs after the login of a user and contains 2 parameter of which one is the username. With the username I can figure out their role.

You still need that username to call wp_login in the first place, the primary difference with wp_signon is that it's passed inside a credentials array, rather than directly as a parameter.

If you had referred to the documentation, you'd have found a simple example taken from the codex:

$creds = array(
    'user_login'    => 'example',
    'user_password' => 'plaintextpw',
    'remember'      => true
);

$user = wp_signon( $creds, false );

if ( is_wp_error( $user ) ) {
    echo $user->get_error_message();
}

Also note:

Note: wp_signon() doesn’t handle setting the current user. This means that if the function is called before the ‘init’ hook is fired, is_user_logged_in() will evaluate as false until that point. If is_user_logged_in() is needed in conjunction with wp_signon(), wp_set_current_user() should be called explicitly.

A confusing point about your question

The question remains, should I use the wp_login() hook?

wp_login() is not a hook, it's an actual function, and it's the function that is deprecated. The hook is not deprecated, and muddling the two is confusing

Your Actual Question, and The One You Should Have Asked

I have a script that I'd like to run when somebody logs in, how do I do this and how do I get the users role?

By specifically asking about your attempted solution, you've constrained and limited the number of answers you could have gotten.

For example, wp_signon is called by wp_login, and the wp_login hook is triggered by wp_signon too:

add_action( 'wp_login', function( $username, \WP_User $user) {
    // the user object has all the information you need, the username is probably not that useful. Anything you do via the username will just fetch the `$user` that you've just been given
}, 10, 2 );

Reading the documentation here:

https://developer.wordpress.org/reference/hooks/wp_login/

We see the following:

Used By #Used By wp-includes/user.php: wp_signon()

  • 1
    Thank you for your in depth answer, I have made a mistake by mixing the function and hook together. If I didn't do that I didn't need to ask this question. I blame the fact that it is Friday. I feel sorry you spend time to create this answer on such a dumb and poorly formulated question. The code never was a problem, the question was: "Should I use a deprecated function?" and the answer is "No." – Tristan .L Jan 12 '18 at 13:47

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.