Skip to content

Instantly share code, notes, and snippets.

@jirsbek
Last active February 25, 2022 13:57
Show Gist options
  • Save jirsbek/5c3fcb992b35b7715eec36441b9b4197 to your computer and use it in GitHub Desktop.
Save jirsbek/5c3fcb992b35b7715eec36441b9b4197 to your computer and use it in GitHub Desktop.
<?php
/**
* Add Image to RSS
*
* Source: https://wordpress.stackexchange.com/a/292763
* Source: https://gist.github.com/DaveyJake/aa59667cd3ced5809b4d
*/
function remote_file_size($url)
{
$data = get_headers($url, true);
if (isset($data['Content-Length']))
return (int) $data['Content-Length'];
}
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, 'article_thumb');
$mime = get_post_mime_type($thumbnail_ID);
if (is_array($thumbnail)) {
echo '<enclosure url="' . $thumbnail[0] . '" length="' . remote_file_size($thumbnail[0]) . '" type="' . $mime . '" data-has-syndication-rights="1" data-portal-copyright="' . get_post_meta($thumbnail_ID, 'hlidacipes_licence_author', true) . '" />';
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment