• dan87manc

    (@dan87manc)


    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

    • This topic was modified 3 years ago by dan87manc.
    • This topic was modified 3 years ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 3 replies - 1 through 3 (of 3 total)
  • whoudini

    (@whoudini)

    Hi there,

    If you add this “if” to your first login check like so it should help:

    if ( $first_login == ‘1’ ) && ( current_user_can( ‘subscriber’ ) )
    echo “this user is a subscriber and it is their first time logging in”;

    If you need to explore this further here is my source:

    https://wordpress.stackexchange.com/questions/60272/find-out-if-logged-in-user-is-not-subscriber

    Hope this helps!

    Thread Starter dan87manc

    (@dan87manc)

    Hey @whoudini

    I appreciate the reply!

    I’ve tried the above and a few different variations but I keep getting:

    ‘Parse error: syntax error, unexpected ‘&&’ (T_BOOLEAN_AND) ..’

    Do you have any ideas for getting around this?

    Thanks again

    whoudini

    (@whoudini)

    My apologies, try this instead:

    if( $first_login == '1' and current_user_can( ‘subscriber’ ) )  {
            // 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;
        }

    Alternatively, if the above does not work; try this:

    if( $first_login == '1' )  {
     if( current_user_can( ‘subscriber’ ) )  {
            // 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;
        }
    }

    Hope this helps!

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.