Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to add post image(s) as enclosure(s) to a wordpress RSS feed item - add to functions.php
<?php
/**
* Add post type images to feed items
*/
add_action('rss2_item', 'add_images_in_feed' );
function add_images_in_feed() {
global $post;
// Thumbnail...
$thumbnail_ID = get_post_thumbnail_id( $post->ID );
if ( $thumbnail_ID ) {
$thumbnail = wp_get_attachment_image_src( $thumbnail_ID, 'full' );
$thumbnail_size = filesize( get_attached_file( $thumbnail_ID ) );
$thumbnail_mime = get_post_mime_type( $thumbnail_ID );
?>
<enclosure url="<?php echo $thumbnail[0] ?>" length="<?php echo $thumbnail_size ?>" type="<?php echo $thumbnail_mime ?>" />
<?php
}
// ...and other attachments
$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 ) {
if ( $thumbnail_ID !== $attachment->ID ) {
$image = wp_get_attachment_image_src( $attachment->ID, 'full' );
$filesize = filesize( get_attached_file( $attachment->ID ) );
$mime = get_post_mime_type( $attachment->ID );
if ( $image ) {
?>
<enclosure url="<?php echo $image[0] ?>" length="<?php echo $filesize ?>" type="<?php echo $mime ?>" />
<?php
}
}
}
}
}
?>
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.