RSS Links In Archive-type Pages

By .     What do you think of this article?

A while back, I adjusted the Feeds List page with basic directions on how to find the RSS feed of a specific category, tag, or author. That would allow readers to subscribe to a specific listing of their choice rather than the whole site feed. While doing that, I had the idea to provide a RSS link directly on the archive pages themselves. The solution wasn’t immediately found, so I shelved that idea for a while. Coming back to it with a fresh mind, I found the solution.

The goal of this script was for it to be completely automatic. A once-and-done deal that I could apply to author, category, tag, and month/year archive pages.

There are WordPress functions that already exist like get category feed link(); but they require specifying the category’s ID. So, I’d have to write a script that uses multiple WP functions to grab the ID of the current archive just so I could add it to a function like this. Not as easy as I thought.

The second option was to try the get permalink(); function, but that didn’t work due to pagination. If I’m on the first page of an archive, say computertechtips.net/category/windows/ I just had to add feed/ and encase the whole think in an anchor tag. It worked, but only for the first page. On page two, the feed link changed to computertechtips.net/category/windows/page/2/feed/ which is an invalid link.

My Solution

I used a predefined PHP veriable to get the current web address, sanitized the URL with a WP function, then wrote a regular expression to remove the page/2/ section. Attach the feed/ and encase it in an anchor tag and that’s it. Here is how it looks on my Category archives page (/wp-content/themes/{MY THEME}/category.php):

<a href="<?php
$ctt_var = esc_url($_SERVER['REQUEST_URI']);
$ctt_var = preg_replace('~page/([0-9]*)/~', '', $ctt_var);;
echo $ctt_var;
?>feed/" title="Subscribe to category">Subscribe to category</a>

Let’s break it down:

  • Line 1 opens the anchor tag and starts the href attribute, which opens the PHP tag
  • Line 2 creates a variable based on the current URI by using the variable built into PHP $_SERVER.
  • It puts that result in the WordPress function esc_url(); which removes invalid/dangerous characters in a process known as sanitation.
  • Line 3 uses the PHP function preg_replace(); to perform a search and replace based on a pattern, known as a regular expression (“RegEx” for short). The function has three parts: the pattern you’re looking for, what you’re replacing it with, and which subject you’re performing it on.
  • You can’t just type page/2/ because 1.) The page number could be any number greater than 1, and 2.) You must add what is called a delimeter at the beginning and end of the RegEx. I use tildes (~) because I’m not looking to search/replace those.
  • What I’m trying to do is remove page/2/ so the replacement is blank. The variable is resaved with the new adjustments made to it.
  • Line 4 prints the variable.
  • Line 5 closes the PHP tag, adds feed/ to the URI, and concludes the anchor tag with an informative title, plus the text being linked.

Compressed Versions

Instead of taking up 5 lines in your code, compress the script down to one now that you understand how it works. Provided below are the compressed versions for all 4 archive types.

/wp-content/themes/{MY THEME}/category.php:
<a href="<?php $ctt_var = esc_url($_SERVER['REQUEST_URI']); $ctt_var = preg_replace('~page/([0-9]*)/~', '', $ctt_var);; echo $ctt_var; ?>feed/" title="Subscribe to category">Subscribe to category</a>

/wp-content/themes/{MY THEME}/tag.php:
<a href="<?php $ctt_var = esc_url($_SERVER['REQUEST_URI']); $ctt_var = preg_replace('~page/([0-9]*)/~', '', $ctt_var);; echo $ctt_var; ?>feed/" title="Subscribe to tag">Subscribe to tag</a>

/wp-content/themes/{MY THEME}/author.php:
<a href="<?php $ctt_var = esc_url($_SERVER['REQUEST_URI']); $ctt_var = preg_replace('~page/([0-9]*)/~', '', $ctt_var);; echo $ctt_var; ?>feed/" title="Subscribe to author">Subscribe to author</a>

/wp-content/themes/{MY THEME}/archive.php:
<a href="<?php $ctt_var = esc_url($_SERVER['REQUEST_URI']); $ctt_var = preg_replace('~page/([0-9]*)/~', '', $ctt_var);; echo $ctt_var; ?>feed/" title="Subscribe to archive">Subscribe to archive</a>

Posted in: Web Development



is the site owner of Computer Tech Tips and is passionate about computer technology, particularly Windows-based software, malware removal, and web development. He enjoys helping people troubleshoot computer problems and providing technical support to family, friends, and people around the net. Xps wrote 79 article(s) for Computer Tech Tips.


 Subscribe to comments: this article | all articles
Comments are automatically closed to prevent SPAM.