einicher.net

The code must do the talking

How to add preview thumbs to your WordPress RSS2-Feed

4. September 2015

I searched a while to find an elegant solution to add preview thumbs to your WordPress RSS2-Feed. Most „solutions“ I found just append the image to the excerpt or content. I didn’t like that very much. I use actions/filters now to append an XML-Element inside the item-Tag. As far as I understand, in RSS2 you use the enclosure-Tag for that, but you can – of corse – change it anyway you want:

function alter_rss2_item()
{
	global $post;
	if (has_post_thumbnail($post->ID)) {
		$thumb_id = get_post_thumbnail_id($post->ID);
		$image = get_post($thumb_id);
		$filesize = filesize(get_attached_file($thumb_id)) ;
		echo '    <enclosure url="'.$image->guid.'" length="'.$filesize.'" type="'.$image->post_mime_type.'" />
		';
	}
}
add_action('rss2_item', 'alter_rss2_item');

Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *