10 Useful RSS-Tricks and Hacks For WordPress

Advertisement

By Jean-Baptiste Jung

RSS is one of those technologies that are extremely simple yet extremely powerful. Currently, RSS is the de facto standard for blog syndication, and it is used widely in both personal and corporate settings; for example, in blogs. And because a large percentage of these blogs run on WordPress, we’ll cover in this post some (hopefully) relatively unknown but useful RSS-related tricks and hacks that will help you use RSS in a more effective way — and without unnecessary and chunky WordPress plug-ins.

Let’s take a look at 10 useful, yet rather unknown RSS-tricks for WordPress. Each section of the article presents a problem, suggests a solution and provides you with an explanation of the solution, so that you can not just solve some of your RSS-related problems but also understand what you are actually doing. Thus, you can make sure your WordPress theme remains under your control and is not bloated with some obscure source code.

1. Control When Your Posts are Available via RSS Link

Screenshot

The problem. Have you ever published an article and then immediately noticed an error? Sure, you can edit it, but there’s another problem: the article has already been published in your RSS feed. To avoid this kind of problem, use this recipe to create a delay between the publication of a post and its availability in your RSS feed.

The solution. To apply this hack, simply paste the following code into your theme’s function.php file. If your theme doesn’t have this file, just create it.

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');

Code explanation. The above code will add a 5-minute delay to the time between when your post is published on your blog and when it appears in your RSS feed. To change the length of the delay, change the value of the $wait variable on line 9.

Sources Link

2. Redirecting WordPress Feeds to FeedBurner Feeds Link

Screenshot

The problem. Beginner bloggers usually start to use FeedBurner only after they have seen it used on many other blogs and realize how useful and cool this tool is. They sign up and start to use it, but their early readers are already subscribed to their default WordPress feed.

Another problem: do you often change your theme? If so, you must be bored having to edit each call to bloginfo(’rss2_url’) and replace it with your FeedBurner feed’s URL.

The solution. The solution to both problems described above is simple: use server redirections.

  1. Create a backup of your .htaccess file, located in the root of your Web server.
  2. Edit the .htaccess file and add the following code. Don’t forget to modify the feed’s URL with your own feed’s URL.
    # temp redirect wordpress content feeds to feedburner
    <IfModule mod_rewrite.c>
     RewriteEngine on
     RewriteCond %{HTTP_USER_AGENT} !FeedBurner    [NC]
     RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
     RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/wprecipes [R=302,NC,L]
    </IfModule>
  3. Save the file. You’re done!

Code explanation. Each time someone clicks on a link to http://www.yourblog.com/feed, he or she will be redirected to http://feeds.feedburner.com/yourblog. This way, you will have never lost an RSS subscriber, and even if you change your theme twice a day, you’ll never have to manually edit your RSS feed links again.

Sources Link

3. Insert Ads (or Anything Else) in Your RSS Feed Link

Screenshot

The problem. Monetizing RSS feeds is currently becoming a common practice, and many blog owners do it to maximize their income. FeedBurner can insert AdSense ads into your feed items, but you need at least 500 subscribers to qualify, and you can’t use any ads other than the AdSense ads provided by FeedBurner.

The solution. It is possible, though, to insert other kinds of ads into your RSS feed. You can, for example, use a link to a free WordPress theme only for your RSS subscribers.

Follow these simple steps to perform this hack:

  1. Edit the functions.php file of your theme. If your theme doesn’t have a functions.php file, simply create one.
  2. Paste the following code into your 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');
    ?>
  3. Save the file. You’re now displaying your ads in your RSS feed!

Code explanation. I have seen many similar hacks on the Web, but all of them require you to edit WordPress core files to achieve the same result. Of course, editing WordPress core files is a very bad idea because then you would have to re-edit the files each time you upgrade your blog. Instead, this hack uses the add_filter() WordPress function to insert content into your RSS feed without editing any core files.

Sources Link

4. Format Your Images for Feed Readers Link

Screenshot

The problem. You took a lot of time to write and format your post and add beautiful screenshots. It looks so good on your blog. Sadly, when the post is displayed in Google Reader or any other RSS reader, it doesn’t look so great.

The solution. This is due to the fact that most feed readers display images inline with text:
inline image
To avoid this problem, add a CSS class to display the image as a block. WordPress provides the built-in class “center“:

<img src="http://www.smashingmagazine.com/images/wordpress-rss-hacks/myimage.jpg" alt="This is my image" class="center"/>

Sources Link

5. Provide Your Readers with a Feed for Each Post Link

Screenshot
The problem. When a post has lots and lots of comments, it can be hard for readers to follow the conversation. Most WordPress users don’t know this, but our favorite blogging engine has a built-in function for providing an RSS feed for the comments in each post.

The solution. Well, this recipe isn’t really a hack or anything: to provide an RSS feed for the comments in a particular post, just call the comment_rss_link() function:

<?php comments_rss_link('&raquo; Comments RSS Feed'); ?>

Sources Link

6. Exclude Categories from Your RSS Feed Link

The problem. Do you use one of your blog categories to let readers know about your website’s news, or does your blog feature a category that has nothing to do with the rest of your content? If so, it is generally not a good idea to include it in your RSS feed.

The solution. Here’s how to get rid of one of the categories in your RSS feed:

  1. First, get the numeric ID of the category you want to exclude. If you don’t know how to get the ID of a particular category, you can learn how here8.
  2. Once you have the ID of the category you want to exclude from your RSS feed, edit the functions.php file in your theme. Create the file if it doesn’t exist.
  3. Paste the following code in it:
    function myFilter($query) {
        if ($query->is_feed) {
            $query->set('cat','-5'); //Don't forget to change the category ID =^o^=
        }
    return $query;
    }
    
    add_filter('pre_get_posts','myFilter');
  4. Save the file, and you’re done!

Code explanation. This hack works exactly the same way as the previous one: create a custom function to exclude the category that you don’t want to appear in your RSS feed, and then use the super-useful add_filter() function to apply it to the pre_get_posts() WordPress core function.

Sources Link

7. Display Any RSS Feed on Your WordPress Blog Link

Screenshot

The problem. Do you have more than one blog, or do you manage a forum? If so, you may want to be able to display any RSS feed on your WordPress blog.

The solution. Many plug-ins can do the job, but they’re not necessary at all. WordPress has a built-in RSS reader that is used, for example, to display news on your dashboard. All you have to do is use it in your theme.

  1. Paste the following code anywhere in your theme (personally, I’d put it in the sidebar, the footer or, even better, the page template11):
    <?php include_once(ABSPATH.WPINC.'/rss.php');
    wp_rss('http://feeds.feedburner.com/wprecipes', 3); ?>
  2. Save it and you’re done. It’s as easy as that!

Code explanation. The first thing we have done is include the rss.php file from WordPress core. This file allows us to use the wp_rss() function, which takes two parameters: the first is the RSS feed’s URL, and the second is the number of RSS entries to be displayed.

Sources Link

8. Use Category-Specific RSS Feeds Link

Screenshot

The problem. Many blogs talk about a lot of different topics: design, programming, blogging tips, etc. Have you ever come across a blog in which you have enjoyed only one category of posts? If so, you should definitely consider offering one feed per category to your own readers.

The solution. Let’s say you’d like to be able to subscribe only to TheGridSystem’s tools section13. The category URL is:

http://www.thegridsystem.org/categories/tools/

To get an RSS feed for this category, you simply have to add /feed to the end of the URL:

http://www.thegridsystem.org/categories/tools/feed

Pretty easy, isn’t it? But pretty useful, too, in my opinion.

9. List RSS Feeds by Category Link

Screenshot

The problem. If you like the previous hack, you will probably also want to be able to display the names of all your category feeds in a list to your readers.

The solution.

  1. Edit any of your theme files, where you want to list your categories and their accompanying feeds.
  2. Paste the following code:
    <?php wp_list_categories('feed_image=http://www.myblog.com/image.gif&feed=XML Feed&optioncount=1&children=0'); ?>
  3. Save the file. You categories will now be displayed, along with their RSS feeds!

Code explanation. This hack uses only the good old wp_list_categories() function, with two parameters. The first is feed_image, which allows us to specify the URL to be displayed as a feed image. The second parameter is feed, which is used to specify the feed format.

10. Get Rid of RSS Feeds the Clean Way Link

Screenshot

The problem. Let’s say you’re using WordPress as a CMS to manage your online portfolio or your company’s website. In such cases, the RSS feed isn’t that useful, and some people would probably want to remove it.

The solution. I have seen many “hacks” on the Web where people say you just have to remove the include on the wp-settings.php core file. I don’t think you should ever edit a core file. Instead, the following hack will do the job. Simply paste this code in the functions.php file of your theme:

function fb_disable_feed() {
  wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
}

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

Sources Link

(al)

Footnotes Link

  1. 1 http://wpengineer.com/publish-the-feed-later/
  2. 2 http://perishablepress.com/press/2008/03/25/redirect-wordpress-feeds-to-feedburner-via-htaccess-redux/
  3. 3 http://www.wprecipes.com/how-to-redirect-wordpress-rss-feeds-to-feedburner-with-htaccess
  4. 4 http://www.wprecipes.com/how-to-insert-ads-on-your-rss-feed
  5. 5 http://www.webinventif.fr/wordpress-ajouter-du-contenu-dans-son-flux/
  6. 6 http://www.pearsonified.com/2007/06/how-to-format-images-for-feed-readers.php
  7. 7 http://www.wprecipes.com/wordpress-how-to-provide-rss-feed-for-each-post-comments
  8. 8 http://www.wprecipes.com/how-to-find-wordpress-category-id
  9. 9 http://www.jangro.com/a/2008/03/01/excluding-posts-from-your-blog-feed/
  10. 10 http://www.wprecipes.com/how-to-exclude-categories-from-your-rss-feed
  11. 11 http://www.wprecipes.com/how-to-create-and-use-wordpress-page-templates
  12. 12 http://www.wprecipes.com/how-to-display-any-rss-feed-on-your-wordpress-blog
  13. 13 http://www.thegridsystem.org/categories/tools/
  14. 14 http://wpengineer.com/disable-wordpress-feed/

↑ Back to top Tweet itShare on Facebook

This guest post was written by Jean-Baptiste Jung, a 28-year-old blogger from Belgium, who blogs about Web Development on Cats Who Code, about WordPress at WpRecipes and about blogging on Cats Who Blog . You can stay in touch with Jean by following him on Twitter.

Advertisement
  1. 1

    Very informative post,
    btw can you say is it possible for us to fuse two different WordPress RSS into one with Feedburner ?
    I mean, let there be two WordPress feeds, like 1) example.com/feed and 2) example.com/second/feed or second.example.com/feed is it possible to fuse 1 and 2 to a single RSS url easily ?
    Simply I want to update readers with just one RSS url, with contents from two WordPress installations side-by-side

    -1
  2. 2

    wow…really helpful, thanks!

    1
  3. 3

    Great resources, thank you~

    0
  4. 4

    thanks for “sm”

    1
  5. 5

    Fantastic post! Thanks very much for this.

    0
  6. 6

    This site always seems to have an excess of amazing resources. I’m a big fan. Thanks for the useful tips – great post :)

    0
  7. 7

    great tutorial………really helpful for people like me who uses wordpress for blogging…

    0
  8. 8

    Very useful article, thanks!

    0
  9. 9

    Thank you, thank you.

    0
  10. 10

    @Ben,
    Try yahoo pipes. http://pipes.yahoo.com/pipes/

    I had to switch to this after feedblendr went under. It’s pretty straightforward once you start playing with it. Shouldn’t take you long.

    Clif

    0
  11. 11

    Hi, for Hack #1, line 20, is that a typo in your code? (assuming pulish should be publish?)

    add_filter('posts_where', 'pulish_later_on_feed');

    Great article regardless, thanks!

    0
  12. 12

    Jean-Baptiste Jung

    December 2, 2008 11:54 am

    @Dan: You’re right, sorry for that.

    @All: Thanks for your comments!

    1
  13. 13

    Direct hit!!! This is excactly what many of us have to learn :) Thank you for this very informative post or better say lesson.

    Cheers

    0
  14. 14

    Some great hacks and used some of them myself. The problem with hacks though is they can break easily when WordPress is upgraded. If you can find plugins that will do these kinds of things then you will be less likely to break your blog when you upgrade.

    0
  15. 15

    Great post!

    Thank you very much.

    0
  16. 16

    Curt Simon Harlinghausen /// Wunderknabe

    December 2, 2008 12:57 pm

    Very helpfull.

    0
  17. 17

    Be careful with the first one. If you have scheduled posts, they’ll all be revealed!! Not good when I have six months of scheduled posts.

    0
  18. 18

    Thanks! This is really useful stuff. I love this site. :D

    0
  19. 19

    Atomised: the web design co-operative

    December 2, 2008 1:22 pm

    Great post. I love RSS and all kinds of feeds. Our company often use a CMS called Symphony (www.symphony21.com) which is natively XML-based and (especially in version 2) can grab and use any XML/RSS based feed SO easily and of course all the data is stored in XML so can create feeds for anything. It is useful to read what is possible with WordPress a CMS I haven’t used for a year or two but am keen to get into again.

    0
  20. 20

    Bill Mulholland :CINQ

    December 2, 2008 1:38 pm

    Priceless, saved me hours of work !

    Keep up the excellent work !

    0
  21. 21

    Exactly what I’m looking for. What about displaying a text that only appear in feed , not blog post?

    0
  22. 22

    Nice collection. It made me remember again a thing I would want: to have feedburner feeds for my main feed (1), 9000 postings (9001), and all tag pages (11001). So in fact have feedburner come up with 11001 different feeds instead of only linking 1 primary feed to feedburner. Would be handy.

    0
  23. 23

    I’ve tried #6, but it doesn’t work. :-(

    Moreover, no hack and no plugin I’ve tried allow me to keep comments for a post from a specific category from showing up in RSS. It’s so annoying.

    I have several categories set aside only for logged-in readers. But if the information of those posts and the comments to those posts show up in the feeds, that defeats the whole purpose.

    The only solution I’ve discovered so far is to disable the comments RSS completely. :-/

    0
  24. 24

    #4 is wrong, rss readers doesn’t support classes from a stylesheet. The solution in the original source you referred is not about adding an additional class but it is about putting your images in a standalone paragraph apart from your text.

    0
  25. 25

    Very useful information.
    Thanks for this post.

    0
  26. 26

    freakin sweet…this is why I love this website…wish you guys went to a physical magazine too.

    0
  27. 27

    Great reference for tweaking RSS!

    As for #7, could I just enter this code:

    into “widgets” and display it on the sidebar. I tried but it shows nothing.

    0
  28. 28

    This is a great post, thanks so much.

    0
  29. 29

    Good stuff.

    I agree with Winnie about #4 though. All you need to do is hit enter a second time after the image; it will have its own paragraph and look better in WordPress and the feed reader.

    0
  30. 30

    Have any idea about how to postpone RSS syndication for Blogger platform? The hack shown works only for the self hosted ones.

    0
  31. 31

    great thanks a lot man, keep rocking ;)

    0
  32. 32

    Kenny | funky house podcast

    December 2, 2008 11:48 pm

    This stuff is gold.

    I host a podcast, and wanted to add images but was worried about formatting in the many rss feeders. Thanks for this!!

    0
  33. 33

    The missing piece in the WordPress puzzle! I’ve been looking for this for months! Thank you Smashing Magazine. I feel I’ve got a good clear picture of WordPress now.

    0
  34. 34

    Echoing LGR (#14 above), I currently do much of the above with plugins.

    If a plugin is not available, hack away. But if you need to upgrade, or if something goes haywire and you forgot to backup, there goes your work while plugins keep things at bay.

    0
  35. 35

    Number 7 is bloody inspired!

    0
  36. 36

    Thanks for link my ideas on WPEngineer.com

    0
  37. 37

    Point 9 about having multiple categories as feeds is great – unless you’re using feedburner for the main website feed.

    How can you have feedburner keep track of multiple feeds, but conglomerate all the feed stats by main web url? So that for each feed subscriber (from different categories) are counted as a subscriber to a feed from my website?

    0
  38. 38

    Some cool tips. Thanks.

    0
  39. 39

    Goooood 4 our info…@##$%^$

    0
  40. 40

    Helpful article, thanks! Just one note: Never use » or « as arrows. These characters are quotation marks.

    0
  41. 41

    I’m surprised no one mentioned tagging the links from the feed to the post itself, using tracking parameters.The example below goes for Google Analytics but it can obviously be adapted to other web analytics mechanisms.Look in your wp-includes/feed-rss.php and replace code for the <link> tag:

    <link><?php the_permalink_rss() ?>?utm_source=blog&utm_medium=rss&utm_content=<?php rawurlencode(the_title_rss()) ?></link>

    Hope that helped!Julien Coquet
    Analytics Country Manager – OX2/LBi
    Visit our blog at http://webanalytics.ox2.eu

    0
  42. 42

    gOOD ….

    0
  43. 43

    Stormy Seas Photography

    December 3, 2008 8:48 am

    This is an excellent post

    0
  44. 44

    Stormy Seas Photography

    December 3, 2008 8:49 am

    This is an excellent post

    http://www.stormyseasphotography.com

    0
  45. 45

    I posted this yesterday, but I think I did something wrong. I turned the 10th tip into a wordpress plugin. (no reason not to) http://clifgriffin.com/2008/12/02/disable-rss-plugin-for-wordpress/

    I may turn some of these other tips into plugins as well…the ones that most simply translate.

    0
  46. 46

    That’s amazing, it’s like you read my mind this morning as I was ferventing googling ‘RSS exclude categories WordPress.’ Smashing Magazine is getting back on top form!

    0
  47. 47

    Great article! Thanks for sharing

    0
  48. 48

    Great Article, very informative! I guess I would be considered a newbie blogger and don’t understand most of what you wrote, but it sounds cool if I could understand it like that! Here is the address to my blog (which I hate with a passion!)

    blog.jonathonhewitt.com

    Just looking for a pointer or two or a place to start learning this stuff. Thanks so much! You can email me at email@jonathonhewitt.com

    0
  49. 49

    Maravilha!
    Precisava de uma informação como esta para dar aquela melhorada em meus feeds.

    0
  50. 50

    Blog for Beginners

    December 3, 2008 7:18 pm

    Hey Jean

    As usual no brings to the table the best WordPress codes better than you – at least you are high in my list whom to turn to for any WordPress related tips and tricks. You rocks, buddy!

    Yan

    0

↑ Back to top