Create an RSS Feed Link for a bbPress Topic

Just a Few Lines of Code

I was asked recently about the ability for bbPress to have an RSS feed link for a single topic. There are existing options to ‘Subscribe’ to a topic as well as the ability to ‘Favorite’ a Topic, but nothing I could find to add an easy RSS link.

Because this community was migrating from an outdated forum and was already familiar with RSS, I decided to see what I could dig up for them.

Unfortunately, my searches brought very few answers. You can add ‘/feed’ to the end of any bbPress url to get the feed but both the site owner and I felt like that was too cumbersome for a busy forum.

I wanted a ‘point-and-click’ answer….and here it is.

If you’re in a hurry here’s the code:

Put this snippet in your functions.php file…

// Make an RSS Feed

function bbpress_rss() {
$url = "http" . (($_SERVER['SERVER_PORT']==443) ? "s://" : "://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo ' <a href="' . $url . 'feed">Subscribe via RSS</a>';
}

Then add this to loop-replies.php where you want your link to appear.

<?php echo bbpress_rss();?>

The Explanation:

$_SERVER is a predefined super global variable.

It is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server.

We’re using the $_SERVER super global to grab the link from the page we’re on.

The we’ll take the values we get and:

  • If we are on port  443 we’ll  add an ‘s’ to ‘http’ since 443 is the  default server port for the https protocol
  • If we’re not on port  443 we’ll use the http by itself
  • Next we add :// to our link
  • The $_SERVER[‘HTTP_HOST’] will add the domain next
  • Then finally the the $_SERVER[‘REQUEST_URI’] will add the actual page that we’re on

All of that will give us a fully formed url that is stored in the $url variable.

Now we need to make that url visible.

To do that we just echo the $url variable inside a link tag.

That’s this line:

echo ' <a href="' . $url . 'feed">Subscribe via RSS</a>';

We’ve appended `/feed` to it so that we can display a pretty RSS link for the end user.

For those of you who are new to PHP the $url variable is a string.

Strings may be concatenated using the ‘.’ (dot) operator.

Note that the ‘+’ (addition) operator will not work for this. See String operators on the php.net website for more information.

Additional Notes:

1) Be sure and create a bbpress directory within your theme folder and copy the files from plugins > bbpress > templates > default > bbpress into that folder.  You can safely modify the files within this new folder without worrying about your changes being over written during plugin updates.

2) I was very new to PHP when I started the migration for these forums and what was a simple tutorial often turned into a series of searches because simple answers often came with many more questions.

Newsletter Signup

Laptop on desk

Ready to get started?

Tell us your story.

Let's Talk