• Resolved seeken

    (@seeken)


    Hello Team,

    I want to redirect users after login based on their user role. For example if user role is premium than it should redirect to /watch page or it should redirect to home page. I am using TuturLMS plugin with Kadence theme and this is my code snippet to redirect:

    function custom_login_redirect() {
    $user = wp_get_current_user();
    // Check if the user is logged in and not an error
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {

    if ( in_array( 'premium', $user->roles ) ) {
    return home_url( '/watch' );
    } else {
    return home_url( '/' );
    }
    }
    }
    add_filter( 'login_redirect', 'custom_login_redirect', 10, 3 );

    However this code snippet does not work, please give me solution. I have also tried LoginWP

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello @seeken,

    Thank you for reaching out to us!

    It seems the code you provided isn’t working for role-based redirection. Please try the following code:

    function tutor_custom_redirect_after_login( $user_id ) {
    // Get the user object
    $user = get_user_by( 'ID', $user_id );

    // Check if user object exists
    if ( $user ) {
    // Check if the user has the role of 'premium'
    if ( in_array( 'premium', (array) $user->roles ) ) {
    // Redirect to /watch for premium
    wp_redirect( home_url( '/watch' ) );
    exit;
    } else {
    // Redirect to /dashboard/ for others
    wp_redirect( home_url( '/dashboard/' ) );
    exit;
    }
    }
    }
    // Hook the function into 'tutor_after_login_success'
    add_action( 'tutor_after_login_success', 'tutor_custom_redirect_after_login' );

    Please try this out and let me know if it resolves the issue!

    Regards,

    Mohammad Nafiz

    Thread Starter seeken

    (@seeken)

    This fixed the issue when we use email and password to login but when we use social login then it uses the default way and redirect to ‘dashboard’ page instead of ‘watch’ page

    • This reply was modified 1 month, 1 week ago by seeken.

    Hi @seeken,

    Thank you for your update! I’m glad to hear that the issue with email and password login has been resolved.

    Unfortunately, at this moment, it is not possible to redirect after social login.

    Please feel free to reach out if you have any further questions!

    Thread Starter seeken

    (@seeken)

    But share any workaround, any changes I can make in code to achieve the desirable behaviour

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.