Skip to:
Content
Pages
Categories
Search
Top
Bottom

Conditional redirect for non-logged in users


  • porcusheep
    Participant

    @porcusheep

    Hi, I need help setting up a redirect for non-logged in users for the members directory and a few specific pages.

    For the members directory I need, it to: If logged in, then allow access to page. If not logged in, then go to landing page.

    For a few specific pages, I need a conditional redirect. And also restrict access to page A for non-logged in users: If logged in, go to page A. If not logged in, go to page B.

    Any help would be greatly appreciated.

Viewing 6 replies - 1 through 6 (of 6 total)

  • Henry Wright
    Moderator

    @henrywright

    Here’s an example which will redirect all unauthenticated members/ page visitors to the homepage.

    add_action( 'init', function() {
        if ( ! is_user_logged_in() && bp_is_members_directory() ) {
            bp_core_redirect( site_url() );
        } else {
            // Do nothing.
        }
    } );

    Feel free to change site_url() to the URL of your landing page.


    porcusheep
    Participant

    @porcusheep

    Thanks Henry!
    That looks like it would work for my members directory but what about doing this for a conditional redirect for a specific post page? What I’m looking for is if they go to a specific post page and they are not logged in, they get to see a different page than if they were logged in.

    Would I do something like this?:

    add_action( 'init', function() {
        if ( ! is_user_logged_in() && get_option(‘siteurl’) . ‘/Page A URL here’  ) {
            bp_core_redirect( "Page B url here" );
        } else {
            //do nothing
        }
    } );

    Also, where do I put this code?


    Henry Wright
    Moderator

    @henrywright

    The code should go in your bp-custom.php file.

    Ref https://codex.buddypress.org/themes/bp-custom-php/

    That looks like it would work for my members directory but what about doing this for a conditional redirect for a specific post page?

    Which post page are you referring to?


    porcusheep
    Participant

    @porcusheep

    I am trying to make it so
    https://www.example.com/example/
    is viewable to logged in members only. If you aren’t logged in, I want people to be redirected to https://www.example.com/example2/

    Also, I just added the code below and I can still access the members directory when not logged in.:

    add_action( 'init', function() {
        if ( ! is_user_logged_in() && bp_is_members_directory() ) {
            bp_core_redirect( site_url().'/register' );
        } else {
            // Do nothing.
        }
    } );

    If I change the code to the one below, I get an endless redirect loop error when trying to load any page of my site regardless if I’m logged in or not…:

    add_action( 'init', function() {
        if ( ! is_user_logged_in() && site_url().'/members'  ) {
            bp_core_redirect( site_url().'/register' );
        } else {
            // Do nothing.
        }
    } );

    Henry Wright
    Moderator

    @henrywright

    Is https://www.example.com/example/ a WordPress page? If it is you can target it with

    if ( is_page( 'example' ) ) {
        // Do something.
    }

    Also, I just added the code below and I can still access the members directory when not logged in

    Where did you add the snippet? The code should work.


    porcusheep
    Participant

    @porcusheep

    I got it working! Thank you so much for you help Henry.
    I ended up modifying the code I found in this post: https://buddypress.org/support/topic/hiding-groups-activity-members-list-to-non-members/

    Ran into some issues as bp_is_page( BP_MEMBERS_SLUG ) is no longer working, replaced it with your bp_is_members_directory()

    Then had to figure out why bp_is_page() doesn’t work, found it is replaced with is_page()

    After, some figuring out how to use is_page(), I got everything working with the code below:

    function nonreg_visitor_redirect() {
    	global $bp;
    
    	if (is_page('alchemist-top-250-companies') ) {
    	
    		if(!is_user_logged_in()) {
    			wp_redirect('/stillneedtosetupage' );
    		}
    		
    	}
    	
    	if (bp_is_members_directory() ) {
    	
    		if(!is_user_logged_in()) {
    			wp_redirect('/register' );
    		}
    	}
    }
    add_filter('get_header','nonreg_visitor_redirect',1);
Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar