BuddyDev

Search

[Resolved] Hide Certain Contents(Pages) From Logged In/Out Users

  • Participant
    Level: Initiated
    Posts: 3
    Emmanuel Johnson on #47778

    Hi,
    My name is Emmanuel and I am new here, I was told of this website by a friend.
    I will really appreciate if I can get some help here.

    I want to hide certain pages from logged out users, redirect them to the login page and then redirect back after successful login.

    I also want to hide certain pages from the logged in users and redirect them to a particular page.

    I don’t want to use a plugin to do this, since I want to reduce the amount of requests that will be sent to the database.

    Perhaps a code in the bp-custom.php file will help, but I have very little knowledge on coding, not to mention wordpress or buddypress.

    Hope I can really get some help from here
    Thank you in advance

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2541
    Ravi on #47784

    Hello Emmanuel,

    Welcome to the BuddyDev Forums. Please try the following code in your ‘bp-custom.php’ file and let me know if it helps or not

    
    /**
     * Redirects site members to specific page if trying to access restricted pages.
     */
    function buddydev_redirect_members_from_restricted_pages() {
        $current_page_id = get_queried_object_id();
    
        if ( ! $current_page_id ) {
            return;
        }
    
        // Page id you want to redirect logged-in users. Replace with your page id.
        $redirection_page_id = 2;
        // Restricted page ids for logged-in users. Replace with your page ids.
        $restricted_page_ids = array( 5, 32, 6 );
    
        if ( in_array( $current_page_id, $restricted_page_ids ) && ! is_page( $redirection_page_id ) ) {
            bp_core_redirect( get_permalink( $redirection_page_id ) );
        }
    }
    
    /**
     * Redirects site visitors to login page if trying to access restricted pages.
     */
    function buddydev_redirect_visitors_from_restricted_pages() {
    
        $current_page_id = get_queried_object_id();
    
        if ( ! $current_page_id ) {
            return;
        }
    
        // do not restrict the signup, activation and login pages
        if ( bp_is_register_page() || bp_is_activation_page() || ( function_exists('is_login') && is_login() ) ) {
            return;
        }
    
        // Restricted page ids for visitors. Replace with yours page ids.
        $restricted_page_ids = array( 29, 30 );
    
        if ( in_array( $current_page_id, $restricted_page_ids ) ) {
            $redirect_url = get_permalink( $current_page_id );
            bp_core_redirect( wp_login_url( $redirect_url ) );
        }
    }
    
    add_action( 'bp_template_redirect', function () {
        // assuming that you are trying to protect 'pages' not other content.
        if ( ! is_page() ) {
            return;
        }
    
         if ( is_user_logged_in() ) {
            buddydev_redirect_members_from_restricted_pages();
        } else {
            buddydev_redirect_visitors_from_restricted_pages();
        }
    } );
    
    

    Note: Replace the page ids with yours

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 3
    Emmanuel Johnson on #47866

    Hi Ravi,
    Thanks a lot, I just noticed and tried it and it worked perfectly, but how do I apply the same to the member profile timeline.

    That is, if a user is logged out and visit another user timeline on the user profile, he/she should be redirected to the login page and then redirected back.

    Once again thank you very much

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2541
    Ravi on #47899

    Hello Emmanuel,

    Thank you for the acknowledgment. Please update the code with:

    
    add_action( 'bp_template_redirect', function () {
    	// assuming that you are trying to protect 'pages' not other content.
    	if ( ! is_page() ) {
    		return;
    	}
    
    	if ( is_user_logged_in() ) {
    		buddydev_redirect_members_from_restricted_pages();
    	} else {
    
    		if ( bp_is_user_activity() ) {
    			$redirect_url = trailingslashit( bp_core_get_user_domain( bp_displayed_user_id() ) ) . bp_get_activity_slug();
    			bp_core_redirect( wp_login_url( $redirect_url ) );
    		}
    
    		buddydev_redirect_visitors_from_restricted_pages();
    	}
    } );
    
    

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 3
    Emmanuel Johnson on #48085

    Hello Ravi, thank you once again.
    The code also works well.

You must be logged in to reply to this topic.

This topic is: resolved