BuddyDev

Search

[Resolved] Altering my buddydev php restrict code to only allow access to a page type

  • Participant
    Level: Enlightened
    Posts: 46
    Kristian Yngve on #24479

    Hi there,

    I need my BuddyDev PHP code that restricts access to all buddypress pages for non-logged in users to allow access for the listings page type ONLY:

    for example:

    https://taipeiexpats.com/members/sponsored-listings/listings/view/24023/globalization-software-engineer-2019-summer-intern/

    and

    https://taipeiexpats.com/members/kristianyngve/listings/view/23988/sap-taiwan-marketing-diploma-student-intern-taipei-taiwan/

    So,

    pages types that go from “https://taipeiexpats.com/members/ [ username] /listings/view/ ….

    to be allowed by non-logged in users.

    May you please help me alter this php code to allow such page types to achieve that?:

    /**
     * Restrict access to BuddyPress for non logged users completely.
     */
    function buddydev_restrict_bp_pages() {
    	// If Logged in
    	// Or not on BuddyPress pages
    	// Or it is register/activation page
    	// Do not restrict.
    	if ( is_user_logged_in() || ! is_buddypress() || bp_is_register_page() || bp_is_activation_page() ) {
    		return;
    	}
    
    	bp_core_redirect( wp_login_url() );
    }
    
    add_action( 'bp_template_redirect', 'buddydev_restrict_bp_pages' );
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #24484

    Hello Kristian,

    Thank you for posting. Try the following code for allow access to listing page for not loggedin users.

    
    /**
     * Restrict access to BuddyPress for non logged users completely.
     */
    function buddydev_restrict_bp_pages() {
    
    	if ( ! is_buddypress() || bp_is_register_page() || bp_is_activation_page() ) {
    		return;
    	}
    
    	if ( is_user_logged_in() ) {
    		return;
    	} elseif ( bp_is_current_component( 'listings' ) ) { // Replace by your component name if not works.
    		return;
    	}
    
    	bp_core_redirect( wp_login_url() );
    }
    
    add_action( 'bp_template_redirect', 'buddydev_restrict_bp_pages' );
    
    

    Please let me know if works or not.

    Regards
    Ravi

    • This reply was modified 4 years, 8 months ago by Ravi.
  • Participant
    Level: Enlightened
    Posts: 46
    Kristian Yngve on #24487

    THANK YOU!!

    It actually works amazingly! 🙂

    Ravi, I can’t thank you enough! Loving the BuddyDev products, and many of them are paid.

    It’s an honor to be part of the BuddyDev community, sir.

    Best,

    Kristian

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2934
    Ravi on #24496

    Hello Kristian,

    Thank you for the kind words. I am glad that I could help.

    Regards
    Ravi

The topic ‘ [Resolved] Altering my buddydev php restrict code to only allow access to a page type’ is closed to new replies.

This topic is: resolved