Last active
December 6, 2022 19:36
-
-
Save jerome-rdlv/638869e1da0c3dba2bdc3973f6df7276 to your computer and use it in GitHub Desktop.
WordPress RSS feed post thumbnail for MailChimp
This file contains 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 | |
/** | |
* 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