Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smeric/e1017a4b66f1d8bb0480 to your computer and use it in GitHub Desktop.
Save smeric/e1017a4b66f1d8bb0480 to your computer and use it in GitHub Desktop.
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