Skip to:
Content
Pages
Categories
Search
Top
Bottom

Redirect non-Logged in Users


  • hclarke20
    Participant

    @hclarke20

    Hi,

    I am having issues with using bbpress. When I post something in the forum, a user who is subscribed to the forum, then receives an email with a link to the post I have just created. However, the user will click on the link and then recieve a 404 page not found message. This is because the forum is private and the user is not currently logged in.

    I was wondering if anyone has a solution to ensure that if a user is not logged into the forum, that they would get the Log In page to access the forum rather than the 404 redirect.

    I cannot put a direct redirect from the post to the login page, because then this would happen for all logged in users too.

    Any help would be greatly appreciated – I am not a PHP expert either, so please explain things in simple terms if changes to the PHP are necessary.

    Thank you.

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

  • grayson_marik
    Participant

    @grayson_marik

    I would love to have a hint on this too!


    johneagle
    Participant

    @johneagle

    Same!


    Robin W
    Moderator

    @robin-w

    I’m looking at this


    grayson_marik
    Participant

    @grayson_marik

    I solved it by using a 404 plugin, which allowed me to generate my own 404 page, which then included links to register / login. Not a sollution but a walk around.


    Robin W
    Moderator

    @robin-w

    ok, bit of code that does redirect but this is via wordpress login- this will be added to my style pack plugin shortly, with some further features, such as bbpress login

    //add private forum check
    add_action( 'bbp_template_redirect', 'rew_access_if_logged_out', 3 );
    
    function rew_access_if_logged_out(){
    	$topic_slug = get_option( '_bbp_topic_slug') ;
    	//quick check if we need to do this function
    	if (strpos($_SERVER['REQUEST_URI'], $topic_slug) == FALSE) return ;
    	$login_check=0 ;
    	$forum_slug = bbp_get_root_slug() ;
    	//if check is set (ie we prefix forums with the forum slug) then part 1 will be forum slug and part 2 will be topic slug, if not part 1 will be topic slug
    	$check = bbp_include_root_slug() ;
    	$link = explode('/',$_SERVER['REQUEST_URI']);
    	//next we need to topic id (post id) of the topic so we need to check if it is a topic and if so, find the topic id
    	if (!is_user_logged_in() && $check && $link[1] == $forum_slug && $link[2] == $topic_slug ) {
    		$post = rew_get_page_by_slug( $link[3], OBJECT, 'topic' );
    		$topic_id =  $post->ID;
    		$login_check=1 ;
    		} 
    	elseif (!is_user_logged_in() && empty($check) && $link[1] === $topic_slug) {
    		$post = rew_get_page_by_slug( $link[2], OBJECT, 'topic' );
    		$topic_id =  $post->ID;		
    		$login_check=1 ;
    	}
    	//now we need to check if the topic belongs to a private forum, so can't be seen
    	if (!empty ($login_check)) {
    		$forum_id = bbp_get_topic_forum_id($topic_id);
    		//if forum is private...
    			if (bbp_get_forum_visibility( $forum_id ) == 'private' ) {
    				$redirect = home_url() . '/wp-login.php?redirect_to=' .  urlencode( $_SERVER['REQUEST_URI'] ); ;
    				wp_redirect( $redirect );
    				exit;
    			}
    	}
    }
    
    function rew_get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page', $status = 'publish' ) { 
      global $wpdb; 
       $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s", $page_slug, $post_type) ); 
         if ( $page ) 
            return get_post($page, $output); 
        return null; 
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar