IF USER IS LOGGED IN AND TRYS TO ACCESS LOGIN FORM PAGE REDIRECT TO SPECIFIC PAGE

I need to be able to redirect a page an already logged in user acesses to a page of my choice..

Not sure how to set this up

  • Nithin

    Hey ME Admin,

    Hope you are doing good today. :slight_smile:

    Could you please try the following code, and then check whether it works to your needs:

    add_action( 'template_redirect', 'wpmu_redirect_to_page' );
    
    function wpmu_redirect_to_page() {
    
    	if ( is_page(2) && is_user_logged_in() ) {
    
    		wp_redirect( 'http://your-website-link/');
    
    		  exit;
    	}
    }

    You can add the following code in your /mu-plugins, and make sure to change the is_page id 2 in the above code to your page id. And it should redirect to the mentioned link as seen in the wp_redirect function.

    Please check the following manual on how to install a /mu-plugins:
    https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    I hope this helps. Please let us know if you still need any further assistance. Have a nice day. :slight_smile:

    Best Regards,
    Nithin

  • ME Admin

    Nathin, one more request..

    I have this code and I need it to recognize user roles

    I have 2 user roles for members, one is subscriber and one is contributor

    if user has role of subscriber and is logged in and they close the tab and then return to the home page at some other time and they are still recognized as logged in, I need to redirect them to the members area I have setup for those with the role of subscribers
    &
    if user has role of contributor and is logged in and they close the tab and then return to the home page at some other time and they are still recognized as logged in, I need to redirect them to the members area I have setup for those with the role of contibutors

    this code works great, except it doesnt have user roles

    add_action('wp_head','wpmy_redirect_logged_in_users_away_from_home');
    function wpmy_redirect_logged_in_users_away_from_home() {
    if( is_user_logged_in() && ( is_home() || is_front_page() ) ) {
    wp_redirect('http://www.google.com');
    exit;
    }
    }

    Just to be clear, I dont want to change the code you created already, I need that and it works great.. just need this code to recognize user roles and redirect each one based on the url I input

    Thanks again for your help!!

  • Rupok

    Hi ME Admin,

    Can you try the following code instead of the previous one? It will redirect based on user role:

    add_action('wp_head','wpmy_redirect_logged_in_users_away_from_home');
    function wpmy_redirect_logged_in_users_away_from_home() {
    	if( is_user_logged_in() && ( is_home() || is_front_page() ) ) {
    	    $current_user   = wp_get_current_user();
    	    $role_name      = $current_user->roles[0];
    		switch ($role_name) {
    		    case "subscriber":
    		        wp_redirect('http://www.google.com');
    		        break;
    		    case "contributor":
    		        wp_redirect('http://www.microsoft.com');
    		        break;
    		}
    	}
    }

    You will need to replace "http://www.google.com" and "http://www.microsoft.com" with your desired URL.

    Please let us know if this doesn't work as expected. We will be glad to help further.

    Have a nice day. Cheers!
    Rupok

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.