• Resolved nickgaskell

    (@nickgaskell)


    How do I redirect users who are already logged in, so that if they try to revisit the login page they are instead redirected to a page ‘/dashboard’.
    I only allow users to register as vendors. So I just need the redirection for users assigned the role of vendor if that makes it any easier?

    • This topic was modified 4 years, 2 months ago by nickgaskell.
    • This topic was modified 4 years, 2 months ago by nickgaskell.
Viewing 2 replies - 1 through 2 (of 2 total)
  • This isn’t specific to Storefront or any theme really as they don’t handle role assignments. That said the following piece of code should get the job done

    function custom_login_screen_detect() {
    	return false !== stripos( wp_login_url(), $_SERVER['SCRIPT_NAME'] );
    }
    
    $login_url = custom_login_screen_detect();
    $current_user = wp_get_current_user();
    
    if ( in_array( 'vendor', (array) $current_user->roles ) && $login_url != false ) {
    	wp_redirect( site_url() . '/test' );
    	exit;
    }

    Regarding customization, you’ll need to 'vendor' over into whatever role you’ve set, make sure to keep it surrounded by the ‘

    For the URL to redirect to, just modify /test to whatever URL you want them to be redirected to. However, you can’t use /dashboard as you mentioned as that is an admin location URL set here:

    https://developer.wordpress.org/reference/functions/wp_redirect_admin_locations/

    So you may have to use something like /controlpanel or similar

    Thread Starter nickgaskell

    (@nickgaskell)

    Thank you for your detailed response. I have implemented the following code and this seems to work as intended:

    add_shortcode(‘woocommerce_my_account_custom’,’woocommerce_my_account_custom’);
    function woocommerce_my_account_custom(){
    if (!is_user_logged_in()) return do_shortcode(‘[woocommerce_my_account]’);
    else {
    $custompage=wp_redirect( site_url() . ‘/dashboard’ );//different page ID which contains custom data for logged in users
    echo apply_filters(‘the_content’,$custompage->post_content);
    }
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Redirect Already Logged In Users’ is closed to new replies.