Interesting Dilemma: Single Forum as “Forum Root Index”
-
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:
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:
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 #1Step 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. 😉
- You must be logged in to reply to this topic.