$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Different home page for logged off users|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Different home page for logged off users

matteradmin9PV0评论

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?

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?

Share Improve this question asked Jan 1, 2017 at 23:29 user86168user86168 211 silver badge4 bronze badges 1
  • And this one won't help wordpress/plugins/buddypress-login-redirect? – prosti Commented Jan 2, 2017 at 1:35
Add a comment  | 

2 Answers 2

Reset to default 2

Can you use this to redirect the logged out users?

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

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

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far