Codeable info

Customizing WordPress Feeds

Posted on by in WordPress Tutorials

Welcome! This is an advanced WordPress tutorial on how to customize WordPress feeds. If you’re not quite sure what feeds are or need to know more about them before reading this more advanced tutorial on WordPress feeds, please read my first tutorial regarding WordPress feeds called Understanding WordPress Feeds. I will not explain the basics of feeds in this tutorial, so if you have any questions about what they are or how they work, you should read my first tutorial about WordPress feeds.

What You Will Learn

In this tutorial, I will show you how to change the look and feel of your WordPress RSS feeds. If you have yet to check out what your RSS feed page looks like, go to it now and have a look. You should be able to view your RSS feeds by adding one of the following URL endings to your WordPress blog’s main URL such as yoursite.com/blog/feed or try these endings until one works:

  • /feed
  • /?feed=rss
  • /?feed=rss2
  • /?feed=atom
  • /?feed=rdf
  • /feed/rss/
  • /feed/rss2/
  • /feed/rdf/
  • /feed/atom/

Ideally, the first URL ending above should work, but might not on some of your blogs because it may not be set up properly. If it doesn’t work, one if the ones below it should. Try each until you find one that works for you. If the first one doesn’t work, refer to my first tutorial I wrote about feeds mentioned above to find out how to fix it.

Also in this tutorial we will cover the following WordPress topics:

  • Feed template tags
  • wp_meta function of WordPress
  • Feed customization plug-ins
  • Building a custom feed template

Prerequisites

Since this is an advanced WordPress tutorial, there are several things about WordPress and coding that you need to know before you will be able to understand everything in the tutorial. Here is a quick list of things you should already know:

  • Feeds – if you don’t have an understanding of what they are, read “Understanding WordPress Feeds” before continuing with this tutorial.
  • PHP – You should have at least some prior experience developing with PHP.
  • HTML – You will need to know basic HTML.
  • JavaScript – You will need only basic knowledge of JavaScript as it plays a minor role in Feed templates.
  • WordPress – You should have at least intermediate experience setting up and editing templates in WordPress.
  • XML – Because we are dealing with feeds and feeds are XML based, it would help to know a little XML, although not mandatory.

Feed Template Tags

There are feed template tags you can use to display the URL of any of the WordPress feeds available. They include ones for RSS, RSS2, Atom, and RDF feeds. Here is an example of each:

RSS - <?php bloginfo('rss2_url'); ?> 
RSS 2.0 - <?php bloginfo('rss2_url'); ?> 
Atom - <?php bloginfo('atom_url'); ?> 
RDF - <?php bloginfo('rdf_url'); ?>

Using them is quite simply. Wherever you place it will display the URL to the corresponding feed so using the previous example would produce:

http://localhost/wordpress/?feed=rss2

You would normally use it in conjunction with a link tag if developing a custom theme. If you are customizing an existing theme however, you will be most likely dealing with the wp_meta WordPress function as we will explain below.

wp_meta Action

the wp_meta function is usually found in the sidebar.php file as it is part of the sidebar widget for most themes. It is also where the feeds are called from, so you will need to familiarize yourself with this function to work with feeds. The wp_meta function is located in the wp_includes directory in the file named general-template.php. If you look at the general-template.php file, you will see the wp_meta function is nothing more than a call to a hook using the do_action function and looks like this:

function wp_meta() {
	do_action('wp_meta');
}

The wp_meta call to action is used to add links to the meta section of the sidebar. So that is how the links get there for the feeds. You can use this method if you want to, but it is probably easier to go around it in make your own links using the feed template tags described earlier, especially if you want to provide custom styles in your links or make them unique in any way. Basically, building your own is easier than editing the functions necessary with the wp_meta call to action.

Custom Feeds

Custom feeds in WordPress can be a real challenge for beginners and pros alike because unless you are very familiar with the way feed readers work and the process they use to present the XML data used in the feeds, customizing them might seem impossible at first. This is why I would recommend using a plug-in if you want to add custom feeds to your WordPress blog. There are a few good plug-ins out there that can handle the task for you quite efficiently. Here are some you could use:

  • Feed Wrangler – will help you for cases where you want to bypass your regular feeds, change your feed URLs or even add or remove ads from feeds. Feed Wrangler can be downloaded from: http://wordpress.org/extend/plugins/feed-wrangler/
  • Feed Template Customize – is another custom feed plug-in that allows you a bit more freedom as far as actual custom styles go. Download this plug-in from: http://wordpress.org/extend/plugins/feed-template-customize/
  • Better Feed – is another feed plug-in you can download that is great for adding things to your feeds. One thing about the default feed templates is that they are not easy to add links, comments, etc. to, so this plug-in may be the solution for you if you just want to add few things to your feeds.

Custom Feeds Without a Plug-in

Okay, I warned you that this wasn’t easy, but if you got this far in the tutorial and still want to do completely custom feeds on your own, I do have a solution for you that isn’t too difficult. My ultimate solution when I had to do this for a client was to create a custom template, give it a URL resembling a feed URL and publish it as a page from the WordPress dashboard. Let me explain. Here is how I did it:

First, make a custom page template with XML feeds in mind. You can use this one, copy it and make changes as you desire or start from scratch if you know enough about it to do so. This is the easiest way to replace default feeds I could find though, so here you go:

<?php
/*
Template Name: Custom Feeds by Ian
*/
 
function shorten_txt($txtstr, $lngth, $toReplace = '...') { 
$txtstr = strip_tags($txtstr);
if(strlen($txtstr) > $lngth) 
return (preg_match('/^(.*)\W.*$/', substr($txtstr, 0, $lngth+1), $matches) ? $matches[1] : substr($txtstr, 0, $lngth)) . $toReplace;   
return $txtstr; 
}
 
function RSS_Date( $timestamp = null ) {
$timestamp = ($timestamp==null) ? time() : $timestamp;
echo date(DATE_RSS, $timestamp);
}
 
$postNo = 5;
$posts = query_posts('showposts='.$postNo);
$lastpost = $postNo - 1;
 
header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0"?>';
?><rss version="2.0">
<channel>
  <title>Ian's Custom RSS 2.0 Feed</title>
  <link>http://jafty.com/</link>
  <description>The latest blog posts from Jafty.com.</description>
  <language>en-us</language>
  <pubDate><?php RSS_Date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
  <lastBuildDate><?php RSS_Date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
  <managingEditor>[email protected]</managingEditor>
<?php foreach ($posts as $post) { ?>
  <item>
    <title><?php echo get_the_title($post->ID); ?></title>
    <link><?php echo get_permalink($post->ID); ?></link>
    <description><?php echo '<![CDATA['.shorten_txt($post->post_content, 500).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
    <pubDate><?php RSS_Date( strtotime($post->post_date_gmt) ); ?></pubDate>
    <guid><?php echo get_permalink($post->ID); ?></guid>
  </item>
<?php } ?>
</channel>
</rss>

There, the bulk of the work is done for you. Here are the steps you need to take next to make this work:

  1. Make the necessary changes to the above code as you see fit. Things you might want to change could include the template name, title, link and description. Then, give it a file name that resembles the template name so you can recognize it later.
  2. Upload the custom feed template to your active WordPress theme. If you are using twentyeleven, upload it to that folder or if not, upload to the folder that has your theme’s name.
  3. Go to your WordPress dashboard and click the link in the left column to add a new page.
  4. You can give the page a title if you want, but do not do anything else except choose the template name you created in the right column towards the bottom of the page then click “publish” to save the page and make it live.
  5. Also you can use the permalink option at the top of the page to give the page it’s own special URL if you so choose, but this is optional.
  6. Now view the page, copy the URL and give it to all your friends and tell them that it is your new custom feed page! You are done.

If you did all the steps correctly, you should have a feed page that looks something like this:

wordpress feeds

After Thoughts

There are some further actions that you may want to look into after using the custom template method described above. For example you may want to consider disabling the default Feeds that you intend to replace. If this is something you would like to try, here is the code you would add to your functions.php file to disable each, but don’t do so unless you are sure you want to disable them and only exclude the ones you are replacing:

remove_action('do_feed_rdf', 'do_feed_rdf', 10, 1);
remove_action('do_feed_rss', 'do_feed_rss', 10, 1);
remove_action('do_feed_rss2', 'do_feed_rss2', 10, 1);
remove_action('do_feed_atom', 'do_feed_atom', 10, 1);

Just put the above code somewhere convenient in the functions.php and it will disable all feeds or use just the lines you want that correspond with the feed you are replacing. In our case we replace only the RSS 2.0 feed, so we used the third line above in green text.

Another interesting option when it comes to feeds is integrating FeedBurner or other online feed services with your WordPress site instead of only using the default feed options. To do this, read the page in the WordPress codex regarding using FeedBurner with WordPress at: http://codex.wordpress.org/Using_FeedBurner

Summary

In this tutorial you should have learned some of the more advanced topics regarding WordPress Feeds. If it was too much for you, start by reading my first tutorial on feeds mentioned above. While customizing templates for feeds is a complex matter, I showed you a viable solution that you can use for substituting default feed actions in WordPress. If you want to actually customize the default theme templates, you can find the files to do so in the wp-includes directory, but I feel my method described at the end of this tutorial is adequate for most people’s needs. Should you feel otherwise, feel free to tackle feeds the hard way, but it won’t be nearly as simple as the solution above. Also, you will probably gain a huge headache learning everything you need to know to do it in that fashion. If someone is an expert on XML feeds and feed readers, then they can surely do it, but the rest of us should probably either use a plug-in or the solution above. Good luck with your WordPress feeds and don’t forget to have fun doing it! If you are not having fun, what are you getting paid for?

)
Codeable info

Comments (6)

Comment by Igor says:

Hi Ian,
I am looking for a solution of problem I have with rss feed on my wordpress page. Recently I noticed that pressing on rss icon (came default with theme) produces comments feeds and not post feeds. I temporary disabled the icon. But I do not want it and I do not know when and how that happened. As I am only trying to post blogs I am not able to find solution of the problem. Did you have such problems and have you found solution?

Comment by Aaron says:

Thanks Ian, exactly what I was looking for.

@Kem, you could probably do something like this: $posts =( array (‘post_type’ => ‘my_posttype’, ‘showposts’ => .$postNo ) );

Comment by kem says:

how would you use this with custom post types?

Comment by Ian Lin says:

Kirk,
Thanks for reading my tutorial! You could use the example in this tutorial as a starting point, but running a feed attached to a multi-site situation is a lot more complex. You would need to run a query on the wp_blogs table to get all of the blog IDs into an array first. Then you would need to write a rather complex loop to query each blog and pull the relative information. I would need more time to give an example, but it can definitely be done. I will post back asap with a better example when I have the time. You can also find answers in the WordPress codex. Basically, you are looking at some method for running query posts from multiple blogs. The good news is that there are functions and even plugins available for this, but if you are like me and like to avoid plugins. then go for doing the multiple query loop method I mentioned earlier. I will try to get back to you with more information asap. In the mean time, think about exactly what you want to display and draw up your specs then it is easier to figure out what exactly you need. Visit us again! Have a nice day – Ian L.

Comment by Kirk Ward says:

Good information Ian …. and a good display of how a feed is built.

I run a WordPress Multisite Network, and I want to create a feed including the home page of each new blog as it is created. I’m not a programmer, but have done fair with WordPress functions and PHP using the CPET (Copy, Paste, Edit and Try) method.

In order to create this feed, I figure I will have to query wp_blogs to identify the newest blog, and then I can run some PHP to get the user meta that shows the profile information I want to include in the feed, such as username, etc.

The feed would be used to display member or subscriber information on another WordPress blog, located on a different server.

Would you mind giving me some ideas about what to change and how it might look when entered? Help and advice would be appreciated.

Kirk Ward

Comment by Rebeca says:

This post is an inspiration for me to study more about this subject. Thankyou, I plan to visit again.

Codeable info