How to: Add Adsense code to a bbPress forum

If you want to add Adsense code to a bbPress forum you’ll find it isn’t as straightforward as you might expect. bbPress has seen a resurgence in popularity lately due to its association with BuddyPress, but plugin development hasn’t yet caught up. In fact, if you’ve gotten accustomed to the ease with which you can maintain and develop your WordPress site, you might find bbPress unexpectedly frustrating. But bbPress does have a few good things going for it: it’s simple and lightweight, and it’s designed to work with WordPress fairly seamlessly. With that in mind bbPress is still a very viable forum option and is becoming moreso as the user base picks up steam again.

In the name of doing my part, this post helps with one of the key issues facing developers of monetized sites who would like to use bbPress – how to add Adsense code to a bbPress forum.

In my attempts to figure this issue out, I came across several resources that almost got me there. Several posts referenced a now defunct plugin called Adsense for bbPress. Several other posts mentioned how to out Adsense code in the header or footer of a bbPress site, but my goal was to have it appear in between posts or interspersed with content in other ways. Finally, I found a post that referenced how to do this on an older version of bbPress. The file references weren’t right for the current version of bbPress, but once I found the places to make the changes, this was a very helpful post.

Finding the place to make those changes was the most difficult aspect and the search was complicated by the fact I use Headway 3 on the site. I wasn’t really sure where to begin looking. After a little bit of exploring and testing, the appropriate files turned out to be here:


In my case, I wanted the loop-replies.php file, but depending on what you want to do, you may need to use a different file in that directory. Also note that if you’re using a bbPress theme, you will replace “bbp-theme-compat” with your active theme name.

After finding the right file, it was fairly simple to add code that would insert the adds. Starting at around line 40, replace the current code with something like this:

Add Adsense code to a bbPress forum

The counters are part of a little bit of code enhancement I did based on Yihui’s work. It ensures that an ad displays on the first post, and then randomly, with a frequency of around .5, on following posts. It stops setting aside the space for the ads once the max of 3 ads is hit.

You can obviously assign some custom classes or play with the span to do some styling. And of course, this approach can be modified to work with any ad service you might be using, not just Google Adsense.

I’m pretty happy with the results; I now have ads showing in between posts in my bbPress forums without too much tweaking. If you’ve got other suggestions for adding ads in bbPress, tell me about them in the comments. 

How to: Add Adsense code to a bbPress forum
1 vote, 5.00 avg. rating (96% score)

Leave a Reply