Home Page Redirection for Logged In Users

Hello!

I hope you are doing great!

Yesterday Tyler helped me with a snipped of code to redirect someone from the homepage if they are logged in. My idea is to, like WPMU DEV does, redirect a user to a different page if they are signed up.

function switch_homepage() {
if( is_user_logged_in() ) {
$page = get_page_by_title( 'Sitios');
update_option( 'page_on_front', $page->ID );
update_option( 'show_on_front', 'page' );
}else {
$page = get_page_by_title( 'About' );
update_option( 'page_on_front', $page->ID );
update_option( 'show_on_front', 'page' );
}
}
add_action( 'init', 'switch_homepage' );

His code, as he said, will create two different homepages. The effect is very similar. For a normal user, this will work correctly. The effect is good. The problem is that wrodpress will set up the “logged in homepage” as default, so that would be the page that google sees.

I would like to tweak this code a little in order to make it a redirection, instead than a change. Similar to when you are logged in a visit https://premium.wpmudev.org/ and get redirected automatically to https://premium.wpmudev.org/profile/private/

Could you help me with this?

    • Carlos
      • The Incredible Code Injector

      Hello @Sajid!

      Thank you for your reply. While "log in redirect" has a good funcionality, it is not the funcionality that i am looking for.

      I dont want to redirect a user afer he logs in. I want a logged in user to get redirected whenever he visits the homepage.

      I hope this clarifies.

      Thank you!

  • Sajid
    • DEV MAN’s Sidekick

    Hi Carlos,

    Hope you are doing good today :slight_smile:

    I am sorry for misunderstanding. To redirect logged in users away from home add below code in functions.php file of your theme or child theme (recommended) and replace the test URL with actual URL of your website.

    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;
        }
    }

    Note you still have to use login redirect plugin for immediate redirect users on custom page after login.

    Cheers, Sajid

    • Carlos
      • The Incredible Code Injector

      Thank you @Sajid!

      That worked perfectly. I jsut modified the following so that I dont get the rediection, only all the other users:

      if( is_user_logged_in() && !is_super_admin() && ( is_home() || is_front_page() ) )

      Thank you for your help!

    • Carlos
      • The Incredible Code Injector

      Hello!

      After using this redirection, I notice that logged in users that visit Domain.com/wp-signup.php are also being redirected.

      I tried to set a static page as the home page, and instead of using "is_home" using "is_page" but it didnt work.

      Maybe using that verification could be based on the current URL, but I dont know how to do it...

      I hope you can help!

  • Sajid
    • DEV MAN’s Sidekick

    Hi Carlos! Hope you are doing good today and welcome back :slight_smile:

    You can use pagenow variable to get the page and modify your condition according to current page.
    global $pagenow. I modified the above code and added global $pagenow condition.

    add_action('wp_head','wpmy_redirect_logged_in_users_away_from_home');
    function wpmy_redirect_logged_in_users_away_from_home() {
    	global $pagenow; 
    
        if( is_user_logged_in() && !is_super_admin()  && $pagenow != 'wp-signup.php' && ( is_home() || is_front_page() ) ) {
    		wp_redirect('http://www.google.com');
    		exit;
        }
    }

    Try this code and let me know how it goes :slight_smile:

    Cheers, Sajid

  • Sajid
    • DEV MAN’s Sidekick

    Hi @Carlos

    Hope you are doing good today :slight_smile:

    Add below code in functions.php file of your theme.

    //allow redirection, even if my theme starts to send output to the browser
    add_action('init', 'do_output_buffer');
    function do_output_buffer() {
            ob_start();
    }

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

  • Sajid
    • DEV MAN’s Sidekick

    Hi @Carlos

    Hope you are doing good today :slight_smile:

    This is a workaround to make the headers error go away. But I don't think there can be any security issues due to using out put buffering, except keeping few bytes in buffer memory.

    Take care and have a nice day :slight_smile:

    Cheers, Sajid

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.