You can use filters in your functions.php file in your theme directory to add content into the RSS feed. Here’s some sample code I was using on a site to add a description, video embed, and show notes to the feed which were all custom fields:
function wpbeginner_postrss($content) {
global $wp_query;
$postid = $wp_query->post->ID;
$description = get_post_meta($postid, 'Description', true);
$video = get_post_meta($postid, 'Video Embed Code', true);
$shownotes = get_post_meta($postid, 'Show Notes', true);
if(is_feed()) {
if($description !== '') {
$content = $description . " " . $video . "<h2>Show Notes</h2>" . $shownotes;
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_excerpt_rss', 'wpbeginner_postrss');
add_filter('the_content', 'wpbeginner_postrss');
Thread Starter
mogito
(@mogito)
Cheers for the response Tim.
Ok i know what you mean. im just trying to get to grips with filters and hooks at the moment.
I have this code added to my functions file. This is to ADD a custom field to the content but it doesn’t seem to work. Can you see anything wrong with this code?
function added_content($content) {
$customimage = get_post_meta($post->ID, 'Images', true);
$content = $content . $customimage ;
return $content;
}
add_filter( 'the_content', 'added_content' );
Thanks again for your help!
You need to define $postid so the function knows where the posts are to pull custom fields from them. Add the following code above the $customimage = XXX line and it should work (though it did take some time for RSS to refresh for me):
global $wp_query;
$postid = $wp_query->post->ID;
Thread Starter
mogito
(@mogito)
Thanks Tim.
i’l give this a try. Yup RSS can be a pain testing 🙂
Cheers for your time!
Struan
Thread Starter
mogito
(@mogito)
Tim,
i added this code to try and add a custom field to my feed but im having no luck…! can you see whats wrong?
function wpbeginner_postrss($content) {
global $wp_query;
$postid = $wp_query->post->ID;
$customimage = get_post_meta($post->ID, 'Images', true);
if(is_feed()) {
if($customimage) {
$content = $content . $customimage ;
}
else {
$content = $content;
}
}
return $content;
}
add_filter('the_excerpt_rss', 'wpbeginner_postrss');
add_filter('the_content', 'wpbeginner_postrss');
Thread Starter
mogito
(@mogito)
what am i doing wrong here?? hmmmm…….?
massive headache! 🙁