Support » Plugins » Generate another feed URL via Rewrite

  • Hi,

    I want to exclude a category from the standard feed, and at the same time provide two additional feeds, one for the excluded category and one for everything. Permalinks are activated.

    The former two are easy, I filter pre_get_posts and exclude the specified category, if is_feed() and !is_category(ID) is true. However, how do I add an additional feed, something like http://domain.tld/feed/all that includes everything? I’m not that experienced with WordPress’ internal rewrite system. I assume I have to add an additional rule and check for some parameter at the pre_get_posts filter to include the specified category…

    To sum it up:

    http://domain.tld/feed – Standard feed without specified category
    http://domain.tld/category/feed – Standard feed for specified category
    http://domain.tld/feed/all – Additional feed with everything.

    Thanks in advance!

    Greets, Jeriko

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    In 2.3.2, feeds are handled by action hooks. So adding a new one is simply a matter of hooking it in.

    function your_do_feed_all($for_comments) {
    // do whatever you want here
    add_action('do_feed_all', 'your_do_feed_all');

    Generally speaking, these functions load a named template file of that type in their include directory, but they don’t have to do that.

    This is what the rss2 feed (the default) does:

    function do_feed_rss2($for_comments) {
    	if ( $for_comments ) {
    		load_template(ABSPATH . WPINC . '/feed-rss2-comments.php');
    	} else {
    		load_template(ABSPATH . WPINC . '/feed-rss2.php');

    As you can see, it loads one of the feed templates in the wp-includes directory. These are more or less just like templates in the theme, they have the Loop and everything else.

    The $for_comments parameter is true if the user is requesting a comments feed, false if they’re not.

    Anyway, if you just want to output an rss2 feed with a different query, then you can do just the same thing. Copy that function into your plugin, add a new query to the beginning of it to make it get all the posts, then load the rss2 template just like it does, to make it output an rss2 feed.

    Oh this is so much easier than hassling with rewrite rules, thank you very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Generate another feed URL via Rewrite’ is closed to new replies.