Custom Feeds in WordPress

There are many reason’s you might need to create a custom feed in WordPress. You may need a feed in an unusual or non-standard format, or you may want to add or edit the content of your feeds. Setting up a custom feed is pretty easy, but poorly documented.

Everything revolves around the add_feed function. This function takes two arguments: a name for your feed and a function to call to create the feed:

add_feed('myFeed', 'myPlugin_create_feed');

The function from the latter argument will handle all of the work of creating your feed. It will have access to the WordPress loop, so after calling have_posts() and the_post(), you can take advantage of all the template tags available.

When to call add_feed

For add_feed to work, you have to wait until WordPress has completely initialized before you call it. You can use the init action hook to accomplish this at the right time.

add_action('init', 'myPlugin_add_feed');
 
function myPlugin_add_feed(  ) {
  add_feed('myFeed', 'myPlugin_create_feed');
}

Changing Rewrite Rules

If you stopped there, WordPress would call myPlugin_create_feed() whenever you go to a page and add the query string feed=myFeed to the end of the URL. Example: http://example.com/?feed=myFeed or http://example.com/?s=foo&feed=myFeed.

If you want to make the URL for your feed look like the other feeds in WordPress (e.g., http://example.com/feed/atom), you have to change the rewrite rules.

To tell WordPress to add new rules, use the generate_rewrite_rules action hook. It will call your function just before it finishes building its rules, with the $wp_rewrite object as an argument. Simply add your new rule to the pre-existing $wp_rewrite->rules array.

function myPlugin_rewrite_rules( $wp_rewrite ) {
  $new_rules = array(
    'feed/(.+)' => 'index.php?feed='.$wp_rewrite->preg_index(1)
  );
  $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

You can add this action hook into your myPlugin_add_feedfunction. Finally, you need to tell WordPress to rebuild its rewrite rules now that it has your additions, using $wp_rewrite->flush_rules().

function myPlugin_add_feed(  ) {
  global $wp_rewrite;
  add_feed('myFeed', 'myPlugin_create_feed');
  add_action('generate_rewrite_rules', 'myPlugin_rewrite_rules');
  $wp_rewrite->flush_rules();
}

Now your customized feed should be accessible at http://example.com/feed/myFeed.

24 thoughts on “Custom Feeds in WordPress”

  1. Thanks for very much for documenting it. I was looking for adding a new feed through WordPress Plugin and this blog post, helped me to get started.

    Regards
    Sudar

  2. helpful post. thanks. Do you have any idea how one would customize the feed so that only posts that have a certain meta_key or meat_value would show up in the feed?

  3. Saved my life man. Have been digging around for a while trying to get past SERPs that kept instructing me on how to add a CUSTOM RSS ICON! ;-)

  4. ok, my question might sound dumb, but i still don’t know where i have to write all those functions. which .php wordpress file I should edit?

  5. @ayashi: It depends. If you’re including it in your theme, you’ll need to edit functions.php in your theme directory. If, on the other hand, you’re writing a plugin, the you need to add it to your main plugin file (whatever you choose to call it).

  6. hi Jonathan, after adding these code into my function.php in my theme:
    ————————-
    add_action(‘init’, ‘myPlugin_add_feed’);

    function myPlugin_rewrite_rules( $wp_rewrite ) {
    $new_rules = array(
    ‘feed/(.+)’ => ‘index.php?feed=’.$wp_rewrite->preg_index(1)
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }

    function myPlugin_add_feed( ) {
    global $wp_rewrite;
    add_feed(‘ifeed’, ‘myPlugin_create_feed’);
    add_action(‘generate_rewrite_rules’, ‘myPlugin_rewrite_rules’);
    $wp_rewrite->flush_rules();
    }
    ———————

    I tried to access my feed using something like this : http://www.mydomain.com/feed/ifeed/
    firefox gave me pop up message that i’m about to download rss+xml file. How come I can’t view it like normal feed? I want to view ifeed just like the way I can view http://www.mydomain.com/feed/

    Thank you.

  7. @ayashi: You may need to include this line in myPlugin_create_feed:

    header('Content-type: text/xml');

    That’s what I did in the DOAJ Export plugin, which uses the above formula for creating a feed. As you can see at http://journal.code4lib.org/issues/issue7/feed/doaj, Firefox lets you view the XML in your browser.

    Of course, text/xml may not be appropriate for your purposes. You might identify what headers WordPress is using for its feeds and duplicate those.

  8. Thank you Jonathan, since you mentioned it, I just realized that I haven’t added any function called “myPlugin_create_feed”. I don’t see in the article how to create this function. What is the code for this function?

  9. @ayashi: That really depends on what you’re trying to do. Again using the DOAJ Export plugin as an example, I basically have a one line function:

    function doaj_export(  ) {
      include('doaj-template.php');
    }

    So doaj-template.php is really where all the action occurs. It’s basically a template comparable to index.php in your theme, using the WordPress Loop and many of the standard template tags. Only, instead of producing XHTML for display in the browser, it creates an XML file.

  10. I got a code that gives me all the products in an RSS Feed from a Store in PHP and I would like to add this PHP RSS Feeds to my posts.
    Can I do it in my posts?
    This is my code:

  11. Thanks for this how-to :)

    One question: I’ve been mucking around with this on WP 2.8.4 and I found that the generate_rewrite_rules part in myPlugin_add_feed is not needed at all for this. If I comment out the add_action like below but leave the rewrite flush in place (a must!!):
    `function myPlugin_add_feed( ) {
    global $wp_rewrite;
    add_feed(‘myFeed’, ‘myPlugin_create_feed’);
    //add_action(‘generate_rewrite_rules’, ‘myPlugin_rewrite_rules’);
    $wp_rewrite->flush_rules();
    }`
    my new feed is still available via (any URI ending with) myFeed and not just with the query string feed=myFeed… I am confused! Any idea what is going on (wrong) here?

  12. I am new to WordPress and had assistance with this site. I want to add the cooliris plugin for a picture viewer. cooliris for WordPress can only use rss feeds and not local/server folders. I need the ability to access a local/server picture folder to that my wife can ftp pictures to the site. Can or how may WordPress direct an rss feed for cooliris to a particular folder. We do not have any use at this time for rss blogs. Although the pictures are of child births and do not particularry want public distribution a private or uniquely names rss would be adaquate. The rss would only be used to feed cooliris.

    http://www.cooliris.com/express/

    David Little
    [email protected]

  13. Is there any way to just create a special RSS feed without getting into PHP? I just want to create one that offers full text and a couple other things.

    1. @Nick – I want the same thing. My feeds are currently excerpt only, and is the only way I want readers to get it. But I’m working on something in which I need my entire articles published via RSS, so I want to make a separate, private RSS file that has the full content. I don’t quite get this “add_feed” function and there’s not enough documentation. Anyone have ideas?

Comments are closed.