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