Reputation: 1401
I´m trying to add some custom values to the WordPress RSS feed. For testing purposes I got this code:
function add_custom_fields_to_rss() {
return "<test>test</test>\n";
}
add_action('rss_item', 'add_custom_fields_to_rss');
add_action('rss_item2', 'add_custom_fields_to_rss');
I have placed this to the bottom of my themes function.php
. When I now try to get the rss with http://example.com/feed there is no test content which I returned in my custom function.
Does anybody know why ?
Upvotes: 1
Views: 1136
Reputation: 121
First thing you need to do is create the new RSS feed in your theme’s functions.php file
add_action('init', 'customRSS');
function customRSS(){
add_feed('feedname', 'customRSSFunc');
}
The above code triggers the customRSS function, which adds the feed. The add_feed function has two arguments, feedname, and a callback function. The feedname will make up your new feed url yourdomain.com/feed/feedname and the callback function will be called to actually create the feed. Make a note of the feedname, as you’ll need this later on. Once you have initialized the feed, you’ll need to create the callback function to produce the required feed, using the following code in your theme’s functions.php file
function customRSSFunc(){
get_template_part('rss', 'feedname');
}
The code above is using the get_template_part function to link to a separate template file, however you can also place the RSS code directly into the function. By using get_template_part, we can keep the functionality separate to the layout. The get_template_part function has two arguments, slug and name, that will look for a template file with the name in the following format, starting with the file at the top (if it doesn’t find the first, it will move on to the second, and so on):
wp-content/themes/child/rss-feedname.php
wp-content/themes/parent/rss-feedname.php
wp-content/themes/child/rss.php
wp-content/themes/parent/rss.php
for detail, you should check out this link http://www.wpbeginner.com/wp-tutorials/how-to-create-custom-rss-feeds-in-wordpress/
Upvotes: 2