0

I creating website and I need to have different homepages for logged in and logged off users. I have page 'Home page' which is set as home page and is filled with information about my site and pictures and simple page 'Home logged in' which was set as BuddyPress activity stream. What I need is: - User A is visiting website but he is not logged in and can see 'Home page' as homepage - User B is visiting website and he is logged in and can see 'Home page logged in' as homepage. I have tried to reach it with this code, by inserting it to my theme functions.php :

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

But this code sometimes when refreshing page or logging off started to show 'Home page logged in' for logged off users. As I readed this happening because this code change homepage in database. So now my question is how to have separate home page for logged off users without changing database every time?

1

2 Answers 2

2

Can you use this to redirect the logged out users?

<?php if ( !is_user_logged_in() { ?>
<?php wp_redirect( 'https://yourdomain.com.au/logoutpage', 302 ); exit; ?>
<?php } ?>

or this solution? setting a specific home page for logged in users

2
  • Redirections lovering your score on gtmetrix. Thanx for one good man he helped my to fix it by swiching content
    – user86168
    Commented Jan 3, 2017 at 23:15
  • Don't forget to vote if you have your answer
    – Pete
    Commented Jan 4, 2017 at 2:28
-2

I am currently using the following code to display a different homepage for logged in and logged out users. You simply need to change the page id to your chosen page within wordpress and add this to your functions.php file:

function switch_homepage() {
    if ( is_user_logged_in() ) {
        $page = 898; // for logged in users
        update_option( 'page_on_front', $page );
        update_option( 'show_on_front', 'page' );

    } else {
        $page = 615; // for logged out users
        update_option( 'page_on_front', $page );
        update_option( 'show_on_front', 'page' );
    }
}
add_action( 'init', 'switch_homepage' );

I would like to set a different homepage by user role however am struggling to get the solution I need.

1
  • 1
    Very bad solution... Updating such a way the option table on each request... The option update and the request are not done in the instant users request your page. Imagine that 2 users request the page in same time, one logged-in, the other not logged-in... WP could serve to the not logged-in user before the logged-in user the page 898 you stored in the option for the logged-in user... Maybe bypass the WP option field reading... There might be a WP hook allowing it.
    – ClemC
    Commented Aug 22, 2017 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.