Skip to content

Instantly share code, notes, and snippets.

@JonasSkjodt
Last active January 12, 2020 21:20
Show Gist options
  • Select an option

  • Save JonasSkjodt/a05a6a3023823504a47513ca79844b00 to your computer and use it in GitHub Desktop.

Select an option

Save JonasSkjodt/a05a6a3023823504a47513ca79844b00 to your computer and use it in GitHub Desktop.
Adding the Wordpress Post's media file to the RSS feed
<?php
// Add namespace for media:image element used below
add_filter( 'rss2_ns', function(){
echo 'xmlns:media="http://search.yahoo.com/mrss/"';
});
// insert the image object into the RSS item
add_action('rss2_item', function(){
global $post;
if (has_post_thumbnail($post->ID)){
$thumbnail_ID = get_post_thumbnail_id($post->ID);
$thumbnail = wp_get_attachment_image_src($thumbnail_ID, '');
if (is_array($thumbnail)) {
echo '<media:content medium="image" url="' . $thumbnail[0] . '" width="' . $thumbnail[1] . '" height="' . $thumbnail[2] . '" />';
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment