First time login redirect – amend code for ‘subscriber’ user role only
-
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( 'https://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’ then they are sent to the welcome page. But I’ve no idea!
Please could someone help me amend this? Thanks a bunch
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘First time login redirect – amend code for ‘subscriber’ user role only’ is closed to new replies.