0

I have two home pages created in WP. One is set as the front page but for only logged-out users. For logged-in users, they see home page 2 as their home page.

However, I want the URL for logged-in users to be the default URL of my site. So a mere redirection doesn't work as I will have "site.com/home-2".

Is there a way I could achieve this? The code below came across doesn't work and breaks my site.

unction switch_homepage() {
    if ( is_user_logged_in() ) {
        $page = 2516; // for logged in users
        update_option( 'page_on_front', $page );
        update_option( 'show_on_front', 'page' );
    } else {
        $page = 2; // for logged out users
        update_option( 'page_on_front', $page );
        update_option( 'show_on_front', 'page' );
    }
}
add_action( 'init', 'switch_homepage' );

?>
3
  • Hmm...maybe it's easier to make if statement on that page to check if user is logged in and display different layout deppending on result of this check. Maybe using get_template_part() function for example) – anton Apr 23 at 19:05
  • Using update_option() to change the settings will change it for every user, not just the user who's viewing the page right now. This solution isn't the way to go. – Pat J Apr 24 at 1:23
  • Yes, using the update_option() to change the settings doesn't work and shows a totally different page as the front page when I log in as a guest. All I am trying to do is to make sure that whatever my frontpage is for both the logged in user and logged out user is the url stays thesame. – Andre Dike Apr 26 at 8:54
0

If you're just trying to display different content to logged-in vs. logged-out users, I'd recommend using the the_content filter.

add_filter( 'the_content', 'wpse_387084_content_selector' );
function wpse_387084_content_selector( $content ) {
    if ( is_front_page() && is_user_logged_in() ) {
        $page = get_post( 2516 );
        $content = $page->post_content;
    }
    return $content;
}

The above code assumes that you've set the "Page on Front" to the page with ID 2, and that's what you want to show to unauthenticated users.

2
  • Thank you Pat J but this didn't work for me. – Andre Dike Apr 26 at 9:04
  • I am using a buddyboss theme and have the activities page for logged-in users set as the home page. But I want it to assume the default site url as its own URL withouth the forward slash just like the homepage for logged-out users. – Andre Dike Apr 26 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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