Redirect Logged in Users to Specific Page

Member is using the current existing redirection code:

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('https://xxxx/cuenta-gratis-1/');
break;
case "contributor":
wp_redirect('https://xxx/mi-escritorio-7-dias/');
break;
}
}
}

Needs to redirect logged in user when the login page is loaded, login page id 4097

  • Nithin
    • Support Wizard

    Hello ME Admin,

    Hope you are doing good today :slight_smile:

    I'm following up the chat conversation in this ticket so that we could get this fixed asap. Please try the following code, instead of your old code, and see whether all the necessary conditions works:

    add_action('template_redirect','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('https://www.xxxx/cuenta-gratis-1/');
    break;
    case "contributor":
    wp_redirect('https://www.xxxx/mi-escritorio-7-dias/');
    break;
    }
    }
    else if ( is_user_logged_in() && is_page( 4097 ) ) {
    
    wp_redirect( 'http://google.com' );
    exit;
    }
    
    }

    Please let me know how the above code works, if not we'll give a closer look. I have masked your site name with xxx in the above URL, please change that accordingly.

    Since your staging website is down, and if you aren't sure on how to bring that up, please send me your FTP, and WP login credentials so that I could fix this out for you.

    You can send credentials by using our secure contact form: https://premium.wpmudev.org/contact/#i-have-a-different-question

    - To Mark to my attention, the subject line should contain only: ATTN: Nithin Ramdas
    -WordPress admin username
    -WordPress admin password
    -login url
    -FTP credentials (host/username/password)
    -link back to this thread for reference
    -any other relevant urls

    Have a nice day. :slight_smile:

    Kind Regards,
    Nithin

  • ME Admin
    • The Crimson Coder

    Hi Nithin,

    That code works great. The only condition that is not being met is based on user role

    Scenario

    If user has the role of subscriber and tries to access pages with id's of 4097, 5976, 5977, 5978 they should be taken to
    https://www.xxxx/cuenta-gratis-1/

    and if user has the role of contributor and tries to access page with id of 4097, 5976, 5977, 5978 they should be taken to https://www.xxxx/mi-escritorio-7-dias/

    Currently if any logged in user access's pages with id's 4097, 5976, 5977, 5978 they are all taken to the same url http://google.com

    The other conditions work perfect

  • Nithin
    • Support Wizard

    Hello ME Admin,

    Hope you are doing good today :slight_smile:

    I have replaced the above code in your staging website, with the following one, and it's working fine now.

    //Redirect Users according to the roles
    add_action( 'template_redirect', 'wpmy_redirect_logged_in_users_away_from_home' );
    
    function wpmy_redirect_logged_in_users_away_from_home() {
    if( is_user_logged_in() ) {
    
    		$current_user = wp_get_current_user();
    		$role_name = $current_user->roles[0];
    		switch ($role_name) {
    
    			case "subscriber":
    
    				if ( ( is_home() || is_front_page() ) || is_page( array( 4097, 5976, 5977,5978 ) ) )
                                     {
    					wp_redirect('http://xxxxxx/cuenta-gratis-1/');
    					exit;
    				}
    
    				break;
    
    			case "contributor":
    
    				if ( ( is_home() || is_front_page() ) ||  is_page( array( 4097, 5976, 5977,5978 ) ) )  {
    					wp_redirect('http://xxxxxx/mi-escritorio-7-dias/');
    					exit;
    				}
    
    				break;
    		}
    
    	}
    }

    Please do test it, and let us know if you still need any further assistance. Have a nice day ahead. :slight_smile:

    Best Regards,
    Nithin

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.