Support » Plugin: bbp style pack » Interesting Dilemma: Single Forum as “Forum Root Index”

  • ResolvedPlugin Author codejp3

    (@codejp3)


    I got an interesting email about how to resolve a “problem” with bbPress where forum users had to make an extra click to reach the forum content.

    I put “problem” in quotes, because for 95%+ bbPress admins, it’s not a problem, it’s the EXPECTED and WANTED default bbPress forum root index behavior. Most forums have several, even hundreds of forums, and the default forum root index allows forum users to navigate those various forums from a single page (styled nicely with bbP Sptyle Pack of course!).

    BUT, what if you only have ONE single forum, and all of your topics/replies are within this one-and-only forum? Do you need the default forum root index? It looks really empty and out of place, doesn’t serve much of a purpose, and in the case of the email I got, it’s “an unwanted extra click forum users have to click to get to the actual forum content”.

    So how do you make the single forum to act as the forum root index, and disable/remove the default bbPress forum root index?

    If anyone has better suggestions than this, I’d love to hear it, but this is the workaround I came up with for the user who asked me this question. I’m sharing it here as reference in case anyone else wants to accomplish the same thing. As of now, there are no plans to include this workaround within the Style Pack plugin, but that could change if enough people chime in and say “yes, I want that, please add it to Style Pack”.

    These are “loose” instructions, and you can adjust them for your needs.

    Step 1.) Decide new “forum root index” slug

    Decide the slug you want to use for your new “fake” forum root index. I’m going to use “forums” for this example. If you want to use a different slug, then instead of “forums” use your preferred slug for EVERY occurrence where forums exists in each of the steps below.

    Step 2.) Create Page to use as your new “forum root index”

    Create a page with the Title “Forums” (or whatever title you want it to have) and give it the slug “forums”. This page should have the shortcode:

     [bbp-single-forum id=XX]


    added to it. NOTE: replace XX with the actual forum ID number for the single forum that contains all of your topics/replies. You can find it in /wp-admin/edit.php?post_type=forum if you are unsure what this forum ID number is.

    THIS PAGE will become your new “fake forum index” to override the default bbPress “forum root index” behavior.

    Step 3.) Set Nav Menu to new “forum root index” page

    That page you just created and added the shortcode to will be accessible at:

    https://yoursite.com/forums/

    You need to make sure the “Forums” menu item in the nav menu points to this new URL. You can add the page to the nav menu (preferred way), or add it as a custom URL.

    Step 4.) Change default bbPress forum root index slug options

    Then in /wp-admin/options-general.php?page=bbpress scroll to the “Forum Root Slug” options, and make sure “Prefix all forum content with the Forum Root slug (Recommended)” is NOT checked. 

    Also change the slug for “Forum Root” to something different like: “bbpress-forums”. It will no longer be used, but we still want to change it to something distinctly unique to prevent conflicts of any kind and to make it stand out if it ever accidentally shows up in breadcrumbs/URL strings for easy future debugging reasons.

    Step 5.) Change default bbPress single forum slugs

    In the “Forum Single Slugs” section just below that, setup the following permalink slugs:

    Forum - 	forum
    Topic - 	forums/topic
    Topic Tag - 	forums/topic-tag
    Topic View - 	forums/view
    Reply - 	forums/reply
    Edit - 		forums/edit
    Search - 	forums/search

    We’re just adding “forums/” in-front of each entry (except for “Forum”) to override the default bbPress behavior. “Forum” is intentionally unmodified and the slug remains “forum” so that we can handle it specifically for breadcrumb/URL reasons (taken care of below in step #8).

    Step 6.) Change default bbPress single forum slugs

    Do the same thing in the “Forum User Slugs” section below that. New values:

    User Base - 		forums/users
    Topics Started - 	forums/topics
    Replies Created -	forums/replies
    Favorite Topics - 	forums/favorites
    Subscriptions - 	forums/engagements

    Just add “forums/” in-front of the existing slugs.

    Step 7.) Remove forum root from breadcrumbs

    Now customize the breadcrumbs using Style Pack. Go to /wp-
    admin/options-general.php?page=bbp-style-pack&tab=breadcrumb and scroll
    down to #2 “Breadcrumb Root” and make sure “Disable Root breadcrumbs” IS CHECKED. We don’t want the default bbPress forum root index within the URL structure anymore, and this will remove it from the breacrumbs.

    Step 8.) Override single forum URL permalink structure

    Final step is to override the default bbPress behavior for a single forum URL permalink structure. If we don’t, the breadcrumbs and URLs generated by default for the “General Forum” would be:

    https://yoursite.com/forum/slug-for-the-one-and-only-single-forum

    Note: “slug-for-the-one-and-only-single-forum” would be the actual slug for the one-and-only forum you have setup. If you don’t know what that slug is, you can find it at /wp-admin/edit.php?post_type=forum

    This would result in TWO URLs pointing to the same content. It will certainly confuse you and your forum users, and if your forum is public and indexed by search engines, it could cause a ranking hit due to the duplicate content. We only want ONE URL pointing to the one-and-only forum, we want it to act as the new “fake” forum root index, and we want it at:

    https://yoursite.com/forums/

    The best way to do this would be with a whole lot of custom code using WordPress and bbPress hooks/filters and change the URL structure BEFORE rendering the page, and for every occurrence where there’s a single forum URL. Very complex and far too involved to post in email or in this forum.

     The next best way to accomplish this is to handle internal redirects to just auto-redirect “/forum/slug-for-the-one-and-only-single-forum” to “/forums” with a “301 Permanently Moved” status. This can be accomplished with the following PHP code snippet:

    // function to handle custom redirection for single forum permalinks
    // needed to remove default bbPress forum root index and use custom "fake" forum root index
    function custom_single_forum_redirect() {
    	
    	
    		/*
    		 * Setup Variables. Should be the only part of this code you need to modify
    		 */
    		$original_slug = 'slug-for-the-one-and-only-single-forum';
    		$new_slug = 'forums';
    	
    	
    		/* DO NOT MODFIY ANYTHING BELOW THIS LINE UNLESS YOU HAVE A REASON AND KNOW WHAT YOU'RE DOING! */
    	
    		// only do this code if on the frontend of the site
    		if ( ! is_admin() ) {
    
    				// by default, set redirect flag as false so no redirection happens
    				$redirect = false;
    
    				// let's dissect and check the requested URL
    				if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
    						// does the URL contain the single forum permalink slug and the only single forum slug? 
    						if ( str_pos( $_SERVER['REQUEST_URI'], '/forum/' . $original_slug ) !== false ) {
    								// set redirect flag to true so we do a redirect
    								$redirect = true;
    								// build out new URL to redirect to
    								$redirect_url = is_ssl() ? 'https://' : 'http://';
    								$redirect_url .= $_SERVER['HTTP_HOST'];
    								$redirect_url .= str_replace( '/forum/' . $original_slug, '/' . $new_slug, $_SERVER['REQUEST_URI'] );
    						}
    				}
    
    				// if redirect flag set, let's do the redirect!
    				if ( $redirect === true ) {
    						wp_redirect( $redirect_url, 301 );
    						exit();
    				}
    		}
    }
    
    // hook into init to process redirection before header
    add_action( 'init', 'custom_single_forum_redirect' );

    NOTE: Change that value for “$original_slug” to match the actual slug of the one-and-only forum. Change the value for “$new_slug” to match the slug you decided on in step #1 above and have been using throughout all of these steps.

    If you are using a proper child theme, you can add it to your theme’s functions.php file. You can also add this code snippet with my preferred plugin of choice – Code Snippets: 
    https://wordpress.org/plugins/code-snippets/

    If you use the Code Snippets plugin, it only needs to be run on the frontend of the site, not admin/both. 

    NOTE: I used PHP code to accomplish this URL redirection, but you could also use .htaccess rules, or a plugin like Redirection: https://wordpress.org/plugins/redirection/

    Step 9.) OPTIONAL – only for public forums – redirect old forum root index to new “fake” forum root index

    If your forum is private and requires logging in to see it, you’re done and can skip this step. If your forum is public and search engines have been indexing it within their search results, then you will also want to setup redirection from the old forum root index slug to the new “fake” forum root index that we just setup.

    NOTE: This step is only necessary if the original bbPress forum root slug that we changed in step #4 does not match the new “fake” forum root slug you decided on in step #1 and have been using throughout these steps. If you were using the default bbPress slug”forums” before we changed it in step #4 and you decided to use a new forum root index slug of “forums” in step #1, then they match, and no redirection needs to be setup. If they do NOT match, then redirection needs to be setup.

    This is essentially adding a new code snippet nearly identical to the one above, but with subtle changes:

    // function to handle redirects for old forum root index to new one
    function custom_forum_root_index_redirect() {
    	
    	
    		/*
    		 * Setup Variables. Should be the only part of this code you need to modify
    		 */
    		$original_slug = 'old-forum-root-index-slug';
    		$new_slug = 'forums';
    	
    	
    		/* DO NOT MODFIY ANYTHING BELOW THIS LINE UNLESS YOU HAVE A REASON AND KNOW WHAT YOU'RE DOING! */
    	
    		// only do this code if on the frontend of the site
    		if ( ! is_admin() ) {
    
    				// by default, set redirect flag as false so no redirection happens
    				$redirect = false;
    
    				// let's dissect and check the requested URL
    				if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
    						// does the URL contain the single forum permalink slug and the only single forum slug? 
    						if ( str_pos( $_SERVER['REQUEST_URI'], '/' . $original_slug ) === 0 ) {
    								// set redirect flag to true so we do a redirect
    								$redirect = true;
    								// build out new URL to redirect to
    								$redirect_url = is_ssl() ? 'https://' : 'http://';
    								$redirect_url .= $_SERVER['HTTP_HOST'];
    								$redirect_url .= str_replace( '/' . $original_slug, '/' . $new_slug, $_SERVER['REQUEST_URI'] );
    						}
    				}
    
    				// if redirect flag set, let's do the redirect!
    				if ( $redirect === true ) {
    						wp_redirect( $redirect_url, 301 );
    						exit();
    				}
    		}
    }
    
    // hook into init to process redirection before header
    add_action( 'init', 'custom_forum_root_index_redirect' );

    NOTE: Change that value for “$original_slug” to match the actual original slug of the bbpress forum root. Change the value for “$new_slug” to match the slug you decided on in step #1 above and have been using throughout all of these steps.

    Again, step #9 only needs to be done if:
    a.) your forum is publicly accessible
    b.) your forum has been indexed by search engines
    c.) the original forum root index slug from step #4 does NOT match the new “fake” forum index slug you decided on using in step #1

    Step 10.) Make a donation to Style Pack for saving you a lot of time and headaches

    That should do it!

    This is the simplest way I could come up with to override the default bbPress forum root index behavior and “replace it” with the single forum index of a single forum. I could think of several far more complicated ways, but this way accomplishes the task utilizing available admin options/settings, and one (or two) simple code snippets.

    Without clear direction and explanation, you could easily “get lost in the sauce” and potentially render your forum or your entire site useless.

    If you fond this topic useful, consider donating to Style Pack, and giving us a 5-star rating while you’re at it. 😉

Viewing 15 replies - 1 through 15 (of 51 total)
  • Plugin Author codejp3

    (@codejp3)

    UPDATED REDIRECT CODE SNIPPET:

    AFFECTS STEP #9 & STEP #10 ABOVE!

    It won’t let me edit the above post, so I’m posting an updated code snippet here.

    Change:

    The only change is that “wp_redirect” and “exit” has been added directly to each specific redirection condition check instead of setting a flag and waiting until the end to do the redirect.

    Reason For Change:

    After more testing of the code, I found that in cases where multiple redirection conditions are ture, the $redirect_url value was being overwritten with the wrong redirect-to string.

    // function to handle custom redirection for single forum permalinks
    // needed to remove default bbPress forum root index and use custom "fake" forum root index
    function custom_single_forum_redirect() {
    	
    	
    		/*
    		 * Setup Variables. Should be the only part of this code you need to modify
    		 */
    		$original_forum_index_slug = 'forums';
    		$original_single_forum_slug = 'slug-for-the-one-and-only-single-forum';
    		$new_slug = 'forums';
    		$single_forum_slug_base = bbp_get_forum_slug();
    		$redirect_old_forum_root_index = false;
    	
    	
    		/* DO NOT MODIFY ANYTHING BELOW THIS LINE UNLESS YOU HAVE A REASON AND KNOW WHAT YOU'RE DOING! */
    	
    		// only do this code if on the frontend of the site
    		if ( ! is_admin() ) {
    
    				// let's dissect and check the requested URL
    				if ( isset( $_SERVER['HTTP_HOST'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
    					
    						// does the URL request start with the single forum permalink slug and the only single forum slug? 
    						// we want to redirect /forum/slug-for-the-one-and-only-single-forum to /forums
    						if ( strpos( $_SERVER['REQUEST_URI'], '/' . $single_forum_slug_base . '/' . $original_single_forum_slug ) === 0 ) {
    								// set redirect flag to true so we do a redirect
    								$redirect = true;
    								// build out new URL to redirect to
    								$redirect_url = is_ssl() ? 'https://' : 'http://';
    								$redirect_url .= $_SERVER['HTTP_HOST'];
    								$redirect_url .= str_replace( '/' . $single_forum_slug_base . '/' . $original_single_forum_slug, '/' . $new_slug, $_SERVER['REQUEST_URI'] );
    								wp_redirect( $redirect_url, 301 );
    								exit();
    						}
    					
    						// does the URL request start with the old forum index slug and single forum permalink slug and the only single forum slug? 
    						// we want to redirect old /forums/forum/slug-for-the-one-and-only-single-forum to /forums
    						if ( strpos( $_SERVER['REQUEST_URI'], '/' . $original_forum_index_slug . '/' . $single_forum_slug_base . '/' . $original_single_forum_slug ) === 0 ) {
    								// set redirect flag to true so we do a redirect
    								$redirect = true;
    								// build out new URL to redirect to
    								$redirect_url = is_ssl() ? 'https://' : 'http://';
    								$redirect_url .= $_SERVER['HTTP_HOST'];
    								$redirect_url .= str_replace( '/' . $original_forum_index_slug . '/' . $single_forum_slug_base . '/' . $original_single_forum_slug, '/' . $new_slug, $_SERVER['REQUEST_URI'] );
    								wp_redirect( $redirect_url, 301 );
    								exit();
    						}
    					
    						// handle redirects for the old forum root index, but only if flag set to true and the original/new slugs do not match
    						if ( ( $redirect_old_forum_root_index === true ) && ( $original_forum_index_slug !== $new_slug ) ) {
    								// does the URL request start with the old forum root index slug? 
    								// we want to redirect /old-forum-root-index-slug to /forums
    								if ( strpos( $_SERVER['REQUEST_URI'], '/' . $original_forum_index_slug ) === 0 ) {
    										// set redirect flag to true so we do a redirect
    										$redirect = true;
    										// build out new URL to redirect to
    										$redirect_url = is_ssl() ? 'https://' : 'http://';
    										$redirect_url .= $_SERVER['HTTP_HOST'];
    										$redirect_url .= str_replace( '/' . $original_forum_index_slug, '/' . $new_slug, $_SERVER['REQUEST_URI'] );
    										wp_redirect( $redirect_url, 301 );
    										exit();
    								}
    						}
    					
    				}
    			
    		}
    }
    
    // hook into init to process redirection before header
    add_action( 'init', 'custom_single_forum_redirect' );

    Also marking as resolved for forum support statistics reason, but feel free to ask questions if you are attempting this override of default bbPress behavior and get stuck.

    Hello @codejp3

    You have again outdone yourself. Thanks for this tutorial which will help me and others who are facing this dilemma.

    This is quick note to confirm i will be posting a message soon since i ran into my first problem while following the instructions, as well as to let you know your email had gone to my email spam folder, and i had incorrectly assumed some things i shouldn’t have. 🙁

    I’ve also replied to the email. Be back later.

    • This reply was modified 6 months, 1 week ago by cj74.
    • This reply was modified 6 months, 1 week ago by cj74.

    Actually let me go ahead and state the problem i have ran into. in your point 2) you had said:

    “Create a page with the Title “General Forum” and give it the slug
    “forums”. This page should have the shortcode [bbp-single-forum id=XX]
    added to it. THIS PAGE will become your new “fake forum index” to
    override the default bbPress “forum root index” behavior.”

    I’m unable to name it general forum. The system is naming it general forum-2. It is adding that number and isn’t allowing me to override it.

    • This reply was modified 6 months, 1 week ago by cj74.
    Plugin Author codejp3

    (@codejp3)

    That’s fine. If I could edit the post, I would, but since I can’t, here’s some clarification:

    The page title can be whatever you want. I used “General Forum” as the example so that it would match your only forum name.

    The important part of Step #2 is that the page slug matches the slug you decided on in Step #1.

    Make sure you use the slug from Step #1 in EVERY step after that. They MUST match in every step. If you decided on something other than “forums” that I used in the example instructions, then you MUST use THAT slug in every step instead of “forums”. To keep is simple, I’d suggest using forums as the slug. But it’s your site, your call.

    And you’re correct. I want it to be general forum. But the question was can we go ahead and create the page with general forum-2 for now and later rename it when everything is done? I am assuming at that point we will be able to delete the old page and rename this one? Any conflicts?

    • This reply was modified 6 months, 1 week ago by cj74.
    Plugin Author codejp3

    (@codejp3)

    Honestly, step #1 has to be first for obvious reasons. The rest of the steps could be done in any order as long as they’re all done.

    @codejp3

    No disagreements on that. Step 1 first! But my question was different. I am trying to mentally map out the steps before doing it so i can understand what the heck i am doing 🙂 So i will follow step 1 and at step 2 i have that question. Is it ok to proceed with “general forum-2” for now?

    Plugin Author codejp3

    (@codejp3)

    Sure. You can make it step 2, or step 10. Doesn’t matter. The point is – until you change the slug from “general forum-2” to “forums” or whatever you decided in step #1, it won’t work.

    I chose the order of the steps for the walkthrough because the first few steps deal with setting up the new structure. The middle steps deal with applying that structure to bbPress, and the last steps deal with frontend URLs. It makes the most sense to do each step, fully/completely, in the order I specified, and that is what I recommend. However, as I said in the last reply, the order of the steps doesn’t really matter and if you really insist on doing #2 last, then fine.

    It’s the entirety of all steps combined that produce the desired result. Once ALL steps are done, you have the desired end result. If even a single step is not done, the desired end result will not work.

    I was actually thinking about step 10 before you had even created this post. 🙂

    A somewhat serious question has come up. Once the changes have been done then the site will no longer have that 1st click ‘landing page’. Since i also have buddybress and have enabled users to create their own forums- these changes will not list any child forums or sub-forums, correct? There would be no central page wherein subforums and child forums including the topics therein can be collated/listed?

    • This reply was modified 6 months, 1 week ago by cj74.
    Plugin Author codejp3

    (@codejp3)

    I did not see any other forums tied to bbPress on your site, so I’d have to know where those user forums are.

    If those user forums are part of the main bbPress root index (did not appear to be so when I looked at your site), then it means you have more than one forum after all, and this method won’t work for you.

    If those user forums exist somewhere else besides the default bbPress root index, then they will continue to function as they did before you apply this method.

    There are no other forums at this time. I was saying- users have that option. So in light of the possibility of it happening in future, the question was asked. Will these changes prevent what i have listed in previous post?

    • This reply was modified 6 months, 1 week ago by cj74.
    Plugin Author codejp3

    (@codejp3)

    Yes and no.

    Yes, these instructions would not work as they are because they assume you only have 1 forum, and that you want to replace the default bbPress forum root index with that single forum.

    No, because you could essentially just add a [bbp-single-forum id=XX] shortcode to the page you create in Step #2 for the user forum category to also show the user-created forums below the General Forum on the same page. Or, you could use steps 1, 2, 3, 8 & 9 to create a separate page for that, and have “two fake forum root indexes” – one for the main forum and one for the user forums.

    You’re right. I didn’t consider that issue when thinking about doing the change. I will start with some baby steps in the next few days and will get back to the experts if run into an issue. Thanks.

    Plugin Author codejp3

    (@codejp3)

    Baby steps is the wrong approach. It’s kinda “all or nothing”.

    Set it to maintenance mode.

    Make a backup of your site as it is.

    Apply all of these changes.

    Test it thoroughly to make sure it works as expected.

    If it works as expected, keep the changes, make another backup with the new changes applied, and take it out of maint. mode to go live with the new changes applied.

    If it doesn’t work as expected, take detailed notes about the issue, start a support topic, and then reapply the saved backup prior to the changes so your users don’t experience unnecessary downtime/issues.

    Baby steps is the wrong approach. It’s kinda “all or nothing”.

    Ha @codejp3

    It was just a figure of speech 🙂

    I just have to take off some time from work man. By evening i am kind of exhausted to work on this till midnight and then having to wake up in the morning. But yes, i will complete the steps in one go.

Viewing 15 replies - 1 through 15 (of 51 total)
  • You must be logged in to reply to this topic.