Last active
January 12, 2020 21:20
-
-
Save JonasSkjodt/a05a6a3023823504a47513ca79844b00 to your computer and use it in GitHub Desktop.
Adding the Wordpress Post's media file to the RSS feed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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