In bbPress, we have these things called views. Views are used to show a certain, well, view of your topics, posts, users, and so on depending on the purpose of the view. Several plugins offer alternative views (e.g., highest rated topics, most posts, etc.).
Fortunately, bbPress comes loaded with a function called bb_register_view() that makes this extremely simple. I’ll be showing you how to create new views in this tutorial.
Some things worth noting:
- Views are displayed according to the
view.phpfile within your theme. - bbPress comes loaded with with two default views: “Topics with no replies” and “Topics with no tags.”
- The My Views plugin creates some neat views and is definitely worth checking out.
Here’s a look at some custom views I’ve create:

Custom bbPress views
The bb_register_view() function
bb_register_view() is the function that handles creating new views. Let’s take a look at its parameters and what we can do with them.
bb_register_view( $view, $title, $query_args = '', $feed = TRUE );
$view is what you’ll name the view. What you put here will also become part of the permalink to the view page.
$title is the title of the view that you’ll see on screen. It’ll show in view lists and likely on your view page. Note: Theme and plugin developers should localize this so it can be translated.
Optional: $query_args should be an array of arguments on how to display the topics and such on the view’s page.
Optional: $feed is whether the view should have its own feed. This is set to true by default.
Creating a custom view
Now that you know how the bb_register_view() function works, it’s time to create a couple of views. The first thing you should do is open your theme’s functions.php. If your theme doesn’t have this file, create one.
We’ll create two new views: Alphabetical (ascending) and Alphabetical (descending). Drop this code in your functions.php file:
<?php
add_action( 'bb_init', 'my_custom_views_init' );
function my_custom_views_init() {
$args = array( 'order_by' => 'topic_title', 'order' => 'ASC' );
bb_register_view( 'alphabetical-ascending', __('Alphabetical: Ascending (A – Z)', 'example'), $args, false );
$args = array( 'order_by' => 'topic_title', 'order' => 'DESC' );
bb_register_view( 'alphabetical-descending', __('Alphabetical: Descending (Z – A)', 'example'), $args, false );
}
?>
In your views list, you should now be able to click on either of these views and see what happens.
Custom view templates
By default, bbPress will use your theme’s view.php template to display a view. Sometimes, you might want to create an entirely custom template though, which is perfectly fine.
For the sake of simplicity, we’ll create two new templates with the same names as our views from above: alphabetical-ascending.php and alphabetical-descending.php. Place those files in your theme’s folder.
Then, add this code to your functions.php file:
<?php
add_action( 'bb_custom_view', 'my_custom_views_templates' );
function my_custom_views_templates( $view ) {
if ( 'alphabetical-ascending' == $view ) {
bb_load_template( 'alphabetical-ascending.php' );
exit();
}
elseif ( 'alphabetical-descending' == $view ) {
bb_load_template( 'alphabetical-descending.php' );
exit();
}
}
?>
Of course, what goes in your custom templates is entirely up to you. Use your imagination.
Note: There may be a more elegant way to load custom view templates, so if you have suggestions, I’d love to hear them.
How to list your views
Most bbPress themes will likely list your views somewhere within the theme, but if yours doesn’t, just drop this code wherever you need to list them:
<ul>
<?php foreach ( bb_get_views() as $view => $title ) : ?>
<li class="view"><a href="<?php view_link( $view ); ?>" title="<?php view_name( $view ); ?>"><?php view_name( $view ); ?></a></li>
<?php endforeach; ?>
</ul>
Views are not pages
On a final note, I wanted to point out that views are not pages. bbPress is not a CMS like WordPress, which is what you’d use to create things like pages. One could use views to create ad hoc pages, but this seems a bit crude.
I can see the need for something like a page feature for forum-only sites, but I’ll leave that discussion for another time.
This is a nice tutorial Justin. Can’t wait to play around with bbPress. I only have a couple of gripes with bbPress before starting.
I’m missing an import-feature like the one we’ve come to know (and love) from WordPress. I run the biggest Danish community on IP-Telephony, but unfortunately it was started on PHP-fusion, and there’s no way of importing to bbPress
The second one is the direct integration with WordPress. I don’t see why Automattic don’t integrate bbPress directly into WordPress, so that bbPress could use WordPress page templates. I see better use for that, than the future integration of MU.
I know this is a bit to the side of your writings, but it’s so seldom that you write about bbPress
Thomas Clausen — I’m sure the import feature will come at some point.
I’ll have to disagree about integration with WordPress. Not everyone that uses bbPress uses WordPress and vice versa (and we definitely don’t need the project split into). Loading both bbPress and WordPress on the same page really taxes your system. I have no real uses for merging them. WordPress is a CMS and bbPress is forum software, which are two completely different things.
@Thomas Clausen – Now that it has hit 1.0, I have started migrating different forums to bbPress.
The secret is phpBB, if you check out the bbPress support forums, there is a script to convert phpBB3 to bbPress.
Since phpBB is very popular, I would be very surprised if you were not able to migrate from PHP-Fusion to phpBB, and then on to bbPress.
@Justin Tadlock – Glad to see some great content covering bbPress, looking forward to more!
@Thomas Clausen
WP’s import/export function and the bbpress script are fine for straight-forward applications. I highly recommend Navicat for more complicated data migration. In fact, I use it to backup my db and to write and mass edit posts b/c it’s a lot faster than wp’s admin panel.
In order to use Navicat you need the capability to directly access your db with an external client. Most isp’s don’t allow this as they are concerned about having their other client’s databases hacked. That said, there are sophisticated hosts that do provide this service you just have to hunt around.
@ Justin
I’m sure you’re right about the DB load, but I bet there’s a way to open and close some doors, so that they’re only used when relevant. But of course I have no idea how to do something like that. But being af sister (or maybe a cousin) project of WordPress, I think it should be easier to make the visual (and DB) integration with WordPress than it is today.
@ JLeuze
That’s great advise, I’ll try playing around with that, and see if I can get it to work.
@ EvilEsq
Thanks a bunch.
That’s also interesting, but a little more demanding than the solution JLeuze has. I’ll go with his solution first, and then I’ll try yours if I fail
And when the change is complete, I’ll start annoying you Justin with a bunch of bbPress question… Can’t wait
First time I heard of bb press. I look forward to trying this.
thank you justin for this tutorial, it has been extremely helpful
Hey Justin,
This was cool… I consider building a forum to enhance my business דם I ‘d like to try this for myself.
Those two templates (named as the views ) might be very usefull for my new forum.
Waiting for ypur coming post !
I could see a use to integrate, or at least have the option to integrate, bbPress and WP, when you’re developing a full-fledged site that incorporates a forum and you’re using WP as the CMS. For instance, sharing the user db between the two, full-site search capability, theming, etc. I’m in the process of developing such a site, and it’s been hell getting the two to play nice with each other.
Pretty nice tutorial, thx
Thanks for your tutorial, I actually need something like this!
Wow I need some time to understand all the process, but I found it very usefull…(when,at last I understood it!
)
Quite interesting as usual, although looks like the guy who’s developped the aforementioned plugin is far from coding properly as you, Justin. Spent the afternoon trying to get rid of the mistakes (about 30 at the beginning) and managed to be currently at 5 after spending most of the afternoon on it (cf. my-views.php and my-views-statistics.php…
F
Oooops, just realized the repetition on my sentence… Down to 4 mistakes while getting rid of the series of as written by the author of the plugin on my-views-statistics.php, although seems obvious that the design will be broken when the databasis is changin’. Could you please give me a way to translate this into CSS please? As usual, thank you so much for your expertise.
Best,
F
Of course, I’ve forgotten about the backticks which I can’t use on my azerty keyboard. The piece of code I meant is like br clear both. Hope you’ll get it although not properly written here.
F
Is it possible to create a custom view for Most Recent Topics and Replies? I’m surprised I can’t find anything like this in the form of a plugin or otherwise.