Skip to content

Instantly share code, notes, and snippets.

@jerome-rdlv
Last active December 6, 2022 19:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jerome-rdlv/638869e1da0c3dba2bdc3973f6df7276 to your computer and use it in GitHub Desktop.
Save jerome-rdlv/638869e1da0c3dba2bdc3973f6df7276 to your computer and use it in GitHub Desktop.
WordPress RSS feed post thumbnail for MailChimp
<?php
/**
* This file can be dropped in wp-content/mu-plugins/ to be activated.
* It will add post thumbnails to the RSS feed.
* The image can then be used in MailChimp templates with the *|RSSITEM:IMAGE|* merge tag.
* This implementation is based on useful indications by [vick](https://stackoverflow.com/a/48549541)
*/
// add xml namespace for media element
add_action('rss2_ns', function () {
echo 'xmlns:media="http://search.yahoo.com/mrss/"';
});
// insert media element inside rss items when post has a thumbnail
add_action('rss2_item', function () {
global $post;
if (!$post || !has_post_thumbnail($post)) {
return;
}
$pid = get_post_thumbnail_id($post->ID);
if (!$pid) {
return;
}
$src = wp_get_attachment_image_src($pid, 'large');
printf(
'<media:content medium="image" url="%s" width="%s" height="%s" />',
wp_get_attachment_image_url($pid, 'large'),
$src[1],
$src[2]
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment