How to add custom nodes to WordPress RSS feed

Recently I was working on a WordPress site and wanted to add a custom node to the WordPress RSS Feeds. In my previous post How to add featured image to WordPress RSS feed we saw how to add a featured image to the post content in RSS Feed. In this post we will see how we can add any node to the RSS Feed. To add a custom node in the RSS feed we just need to add some code to the theme’s functions.php file.

Important: Make sure to add the code to the functions.php file of your theme. Do not add it to the functions.php file in the wp-includes directory.

Here is an example code that you can add to the theme’s functions.php file to add the featured image as a node in the RSS Feed.

// Add the image node to RSS feed
function add_rss_node_image() {
	global $post;
	if(has_post_thumbnail($post->ID)):
		$thumbnail = get_attachment_link(get_post_thumbnail_id($post->ID));
		echo "<image>{$thumbnail}</image>";
	endif;
}

add_action('rss2_item', 'add_rss_node_image');

The above example add the featured image URL in the image node like this <image>featured_image_url</image>. You can add any custom nodes to the RSS Feed.

Here is an example to add the image and thumbnail nodes.

// Add the image and thumbnail node to RSS feed
function add_rss_node_image_thumbnail() {
	global $post;
	if (has_post_thumbnail($post->ID)):
		$image = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
		$thumbnail = wp_get_attachment_thumb_url(get_post_thumbnail_id($post->ID));
		echo "<image>{$image}</image>
		<thumbnail>{$thumbnail}</thumbnail>";
	endif;
}
add_action('rss2_item', 'add_rss_node_image_thumbnail');

Related posts:

  1. How to add featured image to WordPress RSS feed
  2. How to add CSS classes to WordPress menu item
  3. How to remove WordPress version number
  4. How to remove WordPress version parameter from JS and CSS files

1 thought on “How to add custom nodes to WordPress RSS feed”

  1. This is awesome! Thanks. I needed the second code, without the thumbnail. Perfect for adding a image-url to a feed for autoposting to social media like Instagram.

Leave a Reply