Skip to:
Content
Pages
Categories
Search
Top
Bottom

Redirect users who aren’t logged in


  • melek_taus
    Participant

    @melek_taus

    I’ve looked everywhere but a lot of the information I’m finding on Google is out of date. I have the latest WP, Buddypress and bbpress.

    When users who aren’t logged in try to access private content, they get sent to a very generic “Page not found”. I’d like users to be redirected to login or to display the bbp-login shortcode on this generic “Page not found”, and after they have logged in, it would be great if they could be redirected to the page they were trying to access.

    I have looked high and low and can’t figure out where in “the loop” all of this is occurring. How much is bbPress, how much is Buddypress, how much is my custom theme & how much is just vanilla WordPress. Would love any advice or pointers that could help me untangle this knot.

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

  • @mcuk
    Participant

    @mcuk

    Hi,

    Maybe using this would help stop users who aren’t logged in seeing things you don’t want them to in the first place (and therefore they won’t be able to try and access it). Not really the redirect solution you are after but it may be of use. Works with BuddyPress (I don’t use bbPress).

    //Prevents non logged in users seeing pages other than those permitted
    function bpcustom_restrict_bp_pages() {
    	global $bp;
    	
    	//These pages are permitted to logged out users
    	if( bp_is_blog_page() || bp_is_register_page() || bp_is_activation_page() ) {
    		return;
    	}
    	
    	if( !is_user_logged_in() ) {
    		bp_core_redirect( $bp->root_domain );
    	}	
    }
    add_action( 'get_header', 'bpcustom_restrict_bp_pages' );

    danbp
    Participant

    @danbp

    Hi,,
    this snippet is similar to the previous, except that it restrict access to BP components and bbPress (remove thoose you want to keep open)

    function bpfr_guest_redirect() {
    global $bp;
    // enter the slug or component conditional here
    	//if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() || bp_is_directory() || bp_is_user() || bp_is_members_component() )
    	if ( bp_is_activity_component() || bp_is_groups_component() || bp_is_blogs_component() || bp_is_page( BP_MEMBERS_SLUG ) || is_bbpress() ) {
    	
    // not logged in user - user will be redirect to any link to want
    	if( !is_user_logged_in() ) { 
    		//wp_redirect( get_option('siteurl') ); //back to homepage
    		wp_redirect( get_option('siteurl') . '/register' ); // back to register page	
    		//wp_redirect( get_option('siteurl') . '/your_page_title' ); // back to whatever page
    		// wp_redirect( get_option('siteurl') . '/wp-login.php/' );
    	} 
      }
    }
    add_filter('get_header','bpfr_guest_redirect', 1 );

    melek_taus
    Participant

    @melek_taus

    @danbp AWESOME!!! Works great! Would there be any way to exclude the bbpress forum index from this, since that is where my users will be redirected to log in?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Redirect users who aren’t logged in’ is closed to new replies.
Skip to toolbar