Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created two new WordPress RSS feeds in my theme functions:

// Custom Feed 1
add_action('init', 'customRSS');
function customRSS(){ 
add_feed('feedname', 'customRSSFunc');
}
function customRSSFunc(){
get_template_part('rss', 'feedname');
}

// Custom Feed 2 add_action('init', 'customRSS2'); function customRSS2(){ add_feed('feedname2', 'customRSS2Func'); } function customRSS2Func(){ get_template_part('rss', 'feedname2'); }

It's working perfect, however based on the DRY concept I feel that may have a better approach to this code instead of adding the whole function for each new custom feed. Is that true?

The tutorial I followed is this if anyone interested.

share|improve this question

3 Answers 3

Here's what I do to keep my RSS Templates together though depending on your version of PHP this may not work but it's pretty clean. You can add multiple feeds here.

/** Register Our RSS Feeds **/
function my_rss_templates() {
    add_feed( 'feedname2', function(){
        get_template_part('rss', 'feedname2'');
    });

    // Rinse and Repeat...
}
add_action( 'after_setup_theme', 'my_rss_templates' );

PHP has a name for the above method, (calling a function like that) but off the top of my head I can't put a name to it.

Anonymous Function Call - Only available in PHP 5.3 and up.

share|improve this answer

You could move the logic into a class that remembers the names and the template parts.

Example, not tested:

class Custom_Feeds {

    private $feeds = array ();

    /**
     * @param array $feeds
     */
    public function __construct( Array $feeds ) {

        $this->feeds = $feeds;
    }

    /**
     * @wp-hook wp_loaded
     * @return void
     */
    public function register() {

        $names = array_keys( $this->feeds );

        foreach ( $names as $name )
            add_feed( $name, array ( $this, 'load_template' ) );
    }

    /**
     * @wp-hook do_feed_$feedname
     * @return void
     */
    public function load_template() {

        $name = get_query_var( 'feed' );

        get_template_part( 'rss', $this->feeds[ $name ] );
    }
}

$feeds = new Custom_Feeds(
    array (
        'feed1' => 'foo',
        'feed2' => 'bar'
    )
);
add_action( 'wp_loaded', array ( $feeds, 'register' ) );

Besides that, I don’t think feeds are theme territory. They are not styled data usually.

share|improve this answer

Here's a short alternative way (untested) to the other great answers:

add_action( 'init', 'custom_feeds' );

function custom_feeds()
{
    foreach( array( 'feedname1', 'feedname2' ) as $name )
    {
        add_feed( $name, 
            function() use ( $name )
            {
                get_template_part( 'rss', $name );  
            }
        );
    }
}

According to the Codex, we should use the init hook and to remember to flush the rewrite rules for add_feed to work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.