A Better Forum List Widget for bbPress

When bbPress was still a standalone installation, I had tried it out and wasn't really impressed. Things were clunky and it didn't always work the way it was supposed to. After languishing for a few years, Automattic decided to take bbpress and turn it into a plugin, improving the functionality leaps and bounds and making it a strong contender amongst other forum option for WordPress.

The great thing about bbPress is that it works with most themes and automatically skins itself through the theme's CSS. You may have to change a few things here or there to get it to look how you want, but it's pretty much an out of the box solution. The only issue I had was that the Forum List widget didn't actually look like the one in the bbPress support forum.

Forum List widget on bbPress support forum
Forum List widget on bbPress support forum

For some reason, the Forum List widget that comes with bbPress doesn't have the topic count. And it doesn't work that great with forum categories. In the end, I decided to put together a little plugin to add in those features.


The Humble Beginning of a Plugin

If you've ever created a plugin before, you'll know what this little section is for:

If not, that's where you add the plugin name and the required information so that it'll appear correctly on the Plugin admin page of your WordPress installation.


The Widget API

Whenever you create a widget, you need to extend the Widget API. If that sentence doesn't make any sense to you, don't worry. It just means that you need to put a few things in place in order for your widget to work.

This is what the basic Widget API code looks like:

This tutorial isn't really about the ins and outs of the Widget API so I suggest taking a look at the Widgets API in the WordPress Codex.


Extending the Widget API

Let's go through the API code one block at a time. First, you need to add in some basic information about the widget:

Next comes the display code for how the widget will appear on the front end. You need to use wp_list_pages() to organize the categorized forums list. You also need to include a walker to organize the list but I'll get into that a bit later.

There's only one option in the widget so the admin form just needs the following:

With only one input option, the update block just requires a simple check:


Registering the Widget

You've extended the API so now you need to make sure to register the widget or you won't be able to use it at all.


Taking It for a Walk

In order to make sure that your categorized forums appear correctly in the widget, you need to use an advanced walker. You called a walker instance in the wp_list_pages() function above so that you can go through the forums and the categories to make sure they all fall into place.

I suggest reading up on the walker class since it is a little complicated. Thankfully I managed to crack it to get what I needed for the plugin.

The walker also relies on a small conditional named forum_list_widget_has_children() which checks to see if a page has sub-pages.


Let's Add Some Style

It only takes a little bit of CSS to make things look good.

You also have to make sure that you hook into the appropriate action in order to display the stylesheet on the front end.


Conclusion

If you want to see the whole she-bang together in a plugin, you can download it above. Install it and add the new Better Forum List widget to your sidebar and you'll see something like this:

The final result
The final result

Now your list will be better organized and display the total topic count for each forum.

If you have any comments or feedback on anything you read above, please feel free to discuss it below.

Tags:

Comments

Related Articles