Support » Developing with WordPress » Registering multiple custom feeds error

  • unsavouryindividual

    (@unsavouryindividual)


    Hi folks,
    Below is a snippet I got from an external wordpress tutorial site to register a custom RSS feed.

    add_action('init', 'customRSS');
    function customRSS(){
            add_feed('tab', 'customRSSFunc');
    }
    function customRSSFunc(){
            get_template_part('rss', 'tab');
    }
    

    I’d like to expand into multiple RSS feeds, and my code looked exactly like this guy’s code on WordPress stackexchange. It “works perfectly” for him, but I’m getting a php error saying I can’t declare customRSS again. To be very clear, I had the exact same code except with my own RSS title values.

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

    Can someone point out what I’m not seeing here?

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You cannot use the same function name in multiple function declarations through out the same namespace, each one has to be unique. While there is no duplication in the snippet you posted, somewhere else on your site code has also declared customRSS as a function. Try renaming your function my_customRSS or similar. Don’t forget to change the corresponding name in add_action() as well 😉

    Thread Starter unsavouryindividual

    (@unsavouryindividual)

    Thanks @bcworkz I’m more of a javascript guy and function layouts are confusingly similar.
    Can you look at this amended code for my second feed and tell me if there’s something off? Do I add_action again? It doesn’t seem generate the feed or an error, either way.

    add_action('init', 'crRSS');
    function crRSS(){
            add_feed('cr', 'crRSSFunc');
    }
    function crRSSFunc(){
            get_template_part('rss', 'cr');
    }
    • This reply was modified 1 year, 8 months ago by unsavouryindividual. Reason: Old code snippet needed updating
    Moderator bcworkz

    (@bcworkz)

    I don’t know about template_part(). AFAIK we want a full template, not a part. Something like load_template( get_stylesheet_directory() .'/rss-cr.php'); perhaps. That said, template part probably will work, a file gets required either way. I’ve heard the wrong content type header is sent by default. You might want this before loading a template:
    header( 'Content-Type: application/rss+xml' );

    The main reason you don’t see your feed I think is you haven’t flushed the rewrite rules so WP knows to watch for your custom feed request. If you’re not writing a plugin for others to use, the easiest way to flush the rules is to visit the permalinks settings screen. No need to change anything, just load the page. To formally flush from plugins, flush from the plugin’s activation hook.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Registering multiple custom feeds error’ is closed to new replies.