• Blog

    Latest from our Blog

  • Tools

    Our Favourite WP Tools

  • Hosting

    Recommended Web Hosts

  • Coupons

    Get great money off deals

  • Themes

    WordPress Theme Directory

  • Plugins

    WordPress Plugins Directory

  • Promote

    Your WordPress Product

RSS is one of the primary delivery means for your blog’s content, your most loyal readers will likely access it via your RSS feed but despite this most people never give any thought to their RSS feed and how it displays. There are many enhancements and hacks you can make to your feed to provide extra features, earn extra income and help your readers spread your content. In this guide we’re going to look at some of the options open to you.

1. Display Post Thumbnails in the Feed

Lots of themes these days make use of post thumbnails or featured images but these aren’t included in your RSS feed by default. To add them in, add the following to your theme’s functions.php file.

// show post thumbnails in feeds
function diw_post_thumbnail_feeds($content) {
	global $post;
	if(has_post_thumbnail($post->ID)) {
		$content = '<div>' . get_the_post_thumbnail($post->ID) . '</div>' . $content;
	}
	return $content;
}
add_filter('the_excerpt_rss', 'diw_post_thumbnail_feeds');
add_filter('the_content_feed', 'diw_post_thumbnail_feeds');

Source: Digging into WordPress

2. Delay RSS Feed publishing

I seem to do this all the time (I did it on this very post!) you hit “Publish” instead of “Save Draft” or “Preview” on the post you are working on and have to hurridly edit it and put it back into draft status. Or you may notice some glaring error that needs correcting. What this snippet does is delay the updating of your RSS Feed, add the following to your theme’s functions.php file and it will be delayed by 5 minutes:

    * /**
    * * puplish the content in the feed later
    * * $where ist default-var in WordPress (wp-includes/query.php)
    * * This function an a SQL-syntax
    * */
    * function publish_later_on_feed($where) {
    * global $wpdb;
    *
    * if ( is_feed() ) {
    * // timestamp in WP-format
    * $now = gmdate('Y-m-d H:i:s');
    *
    * // value for wait; + device
    * $wait = '5'; // integer
    *
    * // http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
    * $device = 'MINUTE'; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
    *
    * // add SQL-sytax to default $where
    * $where .= " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, '$now') > $wait ";
    * }
    * return $where;
    * }
    *
    * add_filter('posts_where', 'publish_later_on_feed');

Source : WP Engineer

3. Insert anything in to your RSS feed

You can use the following snippet to insert a link back to your site, some advertising, social media options etc. Basically anything you like, add the following to your theme’s functions.php file :

<?php
function insertAds($content) {
    $content = $content.'<hr /><a href="http://www.wprecipes.com">Have you visited WpRecipes today?</a><hr />';
    return $content;
}
add_filter('the_excerpt_rss', 'insertAds');
add_filter('the_content_rss', 'insertAds');
?>

In this example, the custom html inserted is “<hr /><a href=”http://www.wprecipes.com”>Have you visited WpRecipes today?</a><hr />”
Simply change that to anything you like.

Source: WP Recipes

4. Exclude a category from your RSS feed

This is handy if you have a separate category for site news or something you wish to keep separate from your main feed. You will need to find the ID of the category you wish to exclude from your feed. To do this, in WordPress admin go to “Posts” > “Categories” and hover over the one you wish to know, look at your browser’s status bar for the url, at the end will be &tag_ID=10 where 10 is the category ID. Now add the following to your theme functions.php:

function myFilter($query) {
    if ($query->is_feed) {
        $query->set('cat','10'); //Don't forget to change the category ID =^o^=
    }
return $query;
}

add_filter('pre_get_posts','myFilter');

Source: WP Recipes

5. Create posts only for your RSS subscribers

If you would like to offer exclusive content that only RSS subscribers can see (maybe as an incentive to signup?) you need to create a category to house the posts. Find out the category ID like for the last hack and note it down. Then add this to theme’s functions.php :

// show post thumbnails in feeds
function diw_post_thumbnail_feeds($content) {
	global $post;
	if(has_post_thumbnail($post->ID)) {
		$content = '<div>' . get_the_post_thumbnail($post->ID) . '</div>' . $content;
	}
	return $content;
}
add_filter('the_excerpt_rss', 'diw_post_thumbnail_feeds');
add_filter('the_content_feed', 'diw_post_thumbnail_feeds');

Now whenever you add a post to this category, it won’t show up on your blog’s homepage, category page, tag page or any other archive page but will be visible in your RSS Feed exclusively for your RSS subscribers.

Source: Web Developer Plus

6. Show any RSS Feed on your WordPress site

A handy piece of code that uses the WordPress feed parser to display any other RSS feed on your site. Handy to pull in forum feeds or a news feed etc, Just insert the following code into a theme file where you want it to appear :

<?php include_once(ABSPATH . WPINC . '/rss.php');
wp_rss('http://urlofthefeedhere.com/feed', 3); ?>

Source: WP Recipes

7. Display your Average Feed Readers

If you are using Feedburner for your RSS feed you will notice that the public chiclet only displays yesterdays reader count, if you want to show the average over the last 7 days then this is what to do. Put the following in your theme’s functions.php file :

function get_average_readers($feed_id,$interval = 7){
	$today = date('Y-m-d', strtotime("now"));
	$ago = date('Y-m-d', strtotime("-".$interval." days"));
	$feed_url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&dates=".$ago.",".$today;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL, $feed_url);
	$data = curl_exec($ch);
	curl_close($ch);
	$xml = new SimpleXMLElement($data);
	$fb = $xml->feed->entry['circulation'];

	$nb = 0;
	foreach($xml->feed->children() as $circ){
		$nb += $circ['circulation'];
	}

	return round($nb/$interval);
}

Then paste the following where you want the count to appear in your theme, remember to replace wplift with your feed ID :

<?php
$nb = get_average_readers('wplift');
echo "I have ".$nb." RSS readers";
?>

Source: Cats Who Blog

8. Separate RSS Feed for Custom Post types

If you’d like to link to a separate feed for a certain custom post type, use this code :


http://www.yoursite.com/feed/?post_type=book

You can also make a feed out of a custom post type and a taxonomy :


http://www.yoursite.com/feed/?post_type=book&genre=romance

Source: WP Beginner

9. Disable WordPress RSS Feeds completely

If you are creating a site which does not use RSS feeds and want to disable them, it’s pretty easy – just add the following to the theme’s functions.php file:

/**
* disable RSS feed
*/
function wp_disable_feed() {
   wp_die( __('Sorry, no feeds available, return to <a href="'. get_bloginfo('url') .'">homepage</a>') );
}

add_action('do_feed', 'wp_disable_feed', 1);
add_action('do_feed_rdf', 'wp_disable_feed', 1);
add_action('do_feed_rss', 'wp_disable_feed', 1);
add_action('do_feed_rss2', 'wp_disable_feed', 1);
add_action('do_feed_atom', 'wp_disable_feed', 1);

Source: Design Gala

10. Create your own user-defined RSS Feed

If you need a custom RSS feed, like for example, a feed indexing only somes categories + tags, or if you redirected all WordPress RSS feeds to Feedburner but still want to be able to get a category feed, the solution is to use a page template. Save the following code in a file called “custom-feed.php” and upload to your theme directory. Then create a new page in WordPress admin and save it blank and choose “custom-feed.php” as the template.

<?php
/*
Template Name: Custom Feed
*/

$numposts = 5;

function yoast_rss_date( $timestamp = null ) {
  $timestamp = ($timestamp==null) ? time() : $timestamp;
  echo date(DATE_RSS, $timestamp);
}

function yoast_rss_text_limit($string, $length, $replacer = '...') {
  $string = strip_tags($string);
  if(strlen($string) > $length)
    return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;
  return $string;
}

$posts = query_posts('showposts='.$numposts);

$lastpost = $numposts - 1;

header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0"?>';
?><rss version="2.0">
<channel>
  <title>Yoast E-mail Update</title>
  <link>http://yoast.com/</link>
  <description>The latest blog posts from Yoast.com.</description>
  <language>en-us</language>
  <pubDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
  <lastBuildDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
  <managingEditor>joost@yoast.com</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['.yoast_rss_text_limit($post->post_content, 500).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
    <pubDate><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
    <guid><?php echo get_permalink($post->ID); ?></guid>
  </item>
<?php } ?>
</channel>
</rss>

Source: Yoast


Author:

Oliver Dale is the founder of Kooc Media, a small internet company based in the UK. Kooc Media runs several high-profile websites including WPLift, ThemeFurnace and DesignersTalk.

Leave Yours +

15 Comments

  1. Or you could just use the Headway theme, LOL.

  2. Cool nifty bits of code there thanks.

  3. Yeah I found the feedburner average user display as the new one. Thanks for sharing

  4. Yeah I found the feedburner average user display as the new one. Thanks for sharing

  5. Have you noticed the first code for the images in feeds no longer works with 3.2?  Ever since upgrading to 3.2 the code no longer works where it used to prior.  Any ideas what may have changed?

  6. Anonymous

    I want to use the weaver 2010 theme but can’t find RSS icon, don’t all themes have RSS and if not how do I add one?  Thanks

  7. Nice list of hacks thanks.

  8. For #4, you need a dash before the category ID in order to exclude it. Should be: query->set(‘cat’,’-10′);

  9. Disturbsoul

     Hi Oli I dont know everyone is getting this issue or not but your footer is covering the whole page I was not able to see a single word, I am using FF latest that is FF 12 on windows. kindly apply clear both on your footer if you see the same issue.this issue appears in all pages (http://wplift.com/common-wordpress-issues-and-how-to-solve-them) Thanks to firebug I managed to see your site.

  10. Daniel

    how do I add
    tags to the content of the RSS? I need it because I’ve used the ”
    Display Post Thumbnails in the Feed” solution on my blog but now I need to customize the CSS on my magento site that reads the css and gives me a
    tag for the image but nothing for the text.

  11. How would you go about removing the date from the RSS feed?
    I run many of my posts without date in WP, but they still show up with date in the RSS feed. Any way to remove the date from the RSS?

  12. Quick correction: in #4, the need to add a minus sign before the category number, to EXCLUDE it, otherwise, that category will be the ONLY category to appear in your RSS feed:

    $query->set(‘cat’,’-10′);

  13. Hi!
    Great read… I was actually looking for something to fix a RSS problem!
    I use the RSS Image plugin to get images in my RSS
    I use Aweber’s blog-cast to send cool looking teaser to my lists, so they can read ;-)
    But somehow Aweber’s support pointed out that my image and content is in the tag and not in the tag??
    I saw the source code and it’s correct… I then checked out a brand new WP site I setup and it’s the same?
    Is this a new thing that everything get stuffed inside the tag ?? or how do I fix this issue?

    In advance: Thx

    Peter

  14. Any one know how to add “4. EXCLUDE A CATEGORY FROM YOUR RSS FEED” I want to show only 1 category to this function below inc all detalis ?

    Thanks for help!

    add_action( ‘after_setup_theme’, ‘my_rss_template’ );
    /**
    * Register custom RSS template.
    */
    function my_rss_template() {
    add_feed( ‘short’, ‘my_custom_rss_render’ );
    }

    /**
    * Custom RSS template callback.
    */
    function my_custom_rss_render() {
    get_template_part( ‘feed’, ‘short’ );
    }

  • Comments are Closed

Search

Our Sponsors