Mark Thomas Miller

Plugin development: hide an element from an RSS feed

Every now and then, you may need to keep one of your HTML elements from appearing in an RSS feed. WordPress makes this fairly easy because it provides the ability to use filters and is_feed().

Wrap the element you want to hide inside of a span with a special class, such as <span class="rsshide">...</span>. Make sure you remember the closing span tag, and keep in mind that the spelling is important because we’re going to use PHP’s preg_replace to parse the text and look for that exact syntax:

function exclude_element_from_RSS($text) {
    if(is_feed()) {
        $text = preg_replace("/<span class=\"rsshide\">(.*)<\/span>/g", '', $text);
    return $text;
add_filter('the_content', 'exclude_element_from_RSS', 50);
add_filter('the_content_rss', 'exclude_element_from_RSS', 50);
add_filter('the_excerpt', 'exclude_element_from_RSS', 50);
add_filter('the_excerpt_rss', 'exclude_element_from_RSS', 50);

Note: this won’t find the expression if you’re using line breaks in between the span tags, and using regex to parse HTML can be a bad thing. Read below for a more detailed explanation.

When your content loads, exclude_element_from_RSS checks  to see if it’s being pulled for a feed or not. If so, preg_replace finds all matches of your span (with g) and everything inside of it (with .*). Then, it returns the correct output through $text.

This will only be applied when the content is in a feed, leaving your single pages functioning normally.

But should you really be parsing HTML with regex?

In most cases, you shouldn’t be parsing HTML with regular expressions. However, I think there are a (very) few edge cases where this is okay. For instance, I used this method to filter an element that always takes the form of a URL inside of a div (with no exceptions):

<span class="rsshide"><div></div></span>

Because of the simplicity of this problem, I didn’t see the point of implementing an entire engine to do the work, so I wrote this instead. If this were even a tad more complex, I would immediately switch to a more refined approach.