'First time login redirect - amend code only for 'subscriber' user role
I'm using the code below to redirect users to a welcome page upon their first login. It works a treat, but I need to apply it to the 'subscriber' user role only.
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
// insert meta that user not logged in first time
update_user_meta($user_id, 'prefix_first_login', '1');
}
// hook when user logs in
add_action('wp_login', 'your_function', 10, 2);
function your_function($user_login, $user) {
$user_id = $user->ID ;
// getting prev. saved meta
$first_login = get_user_meta($user_id, 'prefix_first_login', true);
// if first time login
if( $first_login == '1' ) {
// update meta after first login
update_user_meta($user_id, 'prefix_first_login', '0');
// redirect to given URL
wp_redirect( 'http://www.example-url.com' );
exit;
}
}
Bearing in mind I don't know anything, to me it seems like I need to add another 'if' element. I.e. If 'it's the user's first-time login' and 'the user has a subscriber role'. But I've no idea!
Please could someone help me amend this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
