11/05/2017

The archive contains older posts which may no longer reflect my current views.

Removing post titles from WordPress RSS feeds

I previously detailed a method of automatically replacing blank post titles so that I didn't have multiple items (posted from the Micro.blog app) listed as '(no title)' in the WordPress back end.

Micro.blog wants posts to not have titles but will treat certain text as blank, such as the date, hence the above.

If a status coming from a blog (via RSS) has a title, or one not in a recognised format, Micro.blog will treat it as a long form post and just display it as a title and link. Not ideal.

Jimmy Baum (@jimmymansaray) wanted to have just the date as status titles but this falls foul of these limitations.

So, how to allow different title formats on your own blog but then pass data to Micro.blog in such a way it displays those statuses correctly?

the_title_rss

WordPress allows you to customise your RSS feed on the fly by accessing different aspects of it and applying a filter. Just like modifying post contents prior to saving or display.

The post title in an RSS feed is logically called using the_title_rss() so we need to find a way to modify this based on the specific set of requirements.

Just as I do, Jimmy uses a category called 'microblog' with a dedicated feed so this is what we need to filter on. After a few tests it seemed the most reliable way was to check against the_category_rss() which lists the categories for the current post as would be displayed in the feed itself, like the below for my previous post:

<category><></category>
<category><></category>
<category><></category>
<category><></category>

We can then simply check for the presence of the required category within that string and return an empty title.

The code to do so should be as follows:

function remove_post_title_rss ( $title ) {
  $categories = get_the_category_rss();
  $pos = strpos($categories, 'microblog');
  if ( $pos != '' ) {
    $title = '';
  }
  return $title;
}

add_filter( 'the_title_rss', 'remove_post_title_rss');

3 comments: click to read or leave your own Comments

# I should really set up a second instance of WordPress rather than test code against my live site.

2 comments: click to read or leave your own Comments