BuddyDev

Search

[Resolved] Different home page for logged in users

  • Participant
    Level: Master
    Posts: 413
    Venutius on #8027

    Hi there, I’m not sure if this is possible.

    I have a static homepage for a site I’m building, but what I would like to do is have a different homepage depending on if the user is logged in or not.

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #8031

    Hi George,
    Thank you for asking. It is a very good question.

    yes, It is easily doable.

    here is the code that you can use to display a different page on home page for logged in user

    
    
    /**
     * When a user is logged in, tell WordPress to use 'page' on front page of the site
     * @param string $value
     *
     * @return string
     */
    function buddydev_set_page_as_front_for_loggedin_user( $value ) {
    	if ( is_user_logged_in() ) {
    		$value = 'page';//page is set as front page
    	}
    
    	return $value;
    }
    add_filter( 'pre_option_show_on_front', 'buddydev_set_page_as_front_for_loggedin_user' );
    
    /**
     * For logged in users, set our static page to act as home page
     *
     * @param $value
     *
     * @return int
     */
    function buddydev_set_context_based_page_on_front( $value ) {
    
    	if( ! is_user_logged_in() ) {
    		return $value;
    	}
    
    	//for logged in user, use page id 9
    	return 5;//change with your own page id.
    }
    add_filter( 'pre_option_page_on_front', 'buddydev_set_context_based_page_on_front' );
    
    

    You can change the page_id with your own id.

    Hope that helps.

    Regards
    Brajesh

  • Participant
    Level: Master
    Posts: 413
    Venutius on #8034

    Thanks Brajesh, wuld I put this in the functions file or bp-custom?

  • Participant
    Level: Master
    Posts: 413
    Venutius on #8035

    /i put it in Functions, worked first time, by chance you picked the right post id! Thanks!

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #8036

    Thank you George. Glad it did 🙂

The topic ‘ [Resolved] Different home page for logged in users’ is closed to new replies.

This topic is: resolved