Forked from supermethod/Wordpress RSS 2.0 image enclosure
Last active
October 14, 2015 14:48
-
-
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
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 | |
/** | |
* 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