Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to add an enclosure to a wordpress RSS feed using the first image of the post [with its actual length attribute value] - add to functions.php
function feedFilter($query) {
if ($query->is_feed) {
add_filter('rss2_item', 'feedContentFilter');
}
return $query;
}
add_filter('pre_get_posts','feedFilter');
/* BEGIN SNIPPET
----------------------------------------- */
// Source URL: http://www.w3bees.com/2013/03/get-remote-file-size-using-php.html
// Original Author: Resalat Haque
/**
* Get Remote File Size
*
* @param string $url as remote file URL
* @return int as file size in byte
*/
function remote_file_size($url) {
# Get all header information
$data = get_headers($url, true);
# Look up validity
if (isset($data['Content-Length']))
# Return file size
return (int) $data['Content-Length'];
}
/* END SNIPPET
----------------------------------------- */
function feedContentFilter($item) {
global $post;
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'numberposts' => 1,
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $attachment) {
$image = wp_get_attachment_image_src($attachment->ID, 'large');
$mime = get_post_mime_type($attachment->ID);
}
}
if ($image) {
echo '<enclosure url="'.$image[0].'" length="'.remote_file_size($image[0]).'" type="'.$mime.'"/>';
}
return $item;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.