wordpress

Getting wp_die error when using add_feed() function of WordPress


When I am trying to add custom feed using add_feed() I am getting given error.

<error>
    <code>wp_die</code>
    <title><![CDATA[WordPress &amp;rsaquo; Error]]></title>
    <message><![CDATA[&lt;strong&gt;Error:&lt;/strong&gt; This is not a valid feed template.]]></message>
    <data>
        <status>404</status>
    </data>
</error>

Please find the code I am trying to use:

function feed2_callback( $is_comment_feed, $feed ) {
    header( 'Content-Type: application/xml; charset=UTF-8', true );
    echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<rss version="2.0"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:atom="http://www.w3.org/2005/Atom"
>
    <title>Test</title>
</rss>
<?php
}

add_feed( '_feed2', 'feed2_callback' );

I am getting error when I am checking http://localhost/_feed2


Solution

  • This is happening because in your code $feedname parameter is started with '_' which is creating the issue so once you replace

    add_feed( '_feed2', 'feed2_callback' );
    

    with

    add_feed( 'feed2', 'feed2_callback' );
    

    in your code it should work.

    Also the similar is mentioned in the function doc.

    Now you can access your feed without any error using http://localhost/feed2