Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DaveyJake/aa59667cd3ced5809b4d to your computer and use it in GitHub Desktop.
Save DaveyJake/aa59667cd3ced5809b4d to your computer and use it in GitHub Desktop.
WP RSS 2.0 Image Enclosure with File Size
<?php
/**
* Wordpress RSS Image Enclosure
*
* Wrap ONLY the featured image inside 'enclosure' tags.
*
* @author Davey Jacobson
* @link https://www.daveyjake.dev
*
* @package DJ_Dev
*/
defined( 'ABSPATH' ) || exit;
// Filter the posts before they're sent to the RSS feed.
add_filter( 'pre_get_posts', 'feed_filter' );
/**
* Apply this code only to the RSS feed.
*
* @since 1.0.0
*
* @param WP_Query $query The current WP_Query instance.
*
* @return WP_Query|void Return modified feed if successful. Unmodified if not.
*/
function feed_filter( $query ) {
if ( $query->is_feed ) {
add_filter( 'rss2_item', 'feed_content_filter');
}
return $query;
}
/**
* Add an enclosure to a wordpress RSS feed using the first image of the post
* [with its actual length attribute value] via {@see 'pre_get_posts'}.
*
* @global WP_Post|object $post Current post object.
*/
function feed_content_filter() {
global $post;
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => 'image',
'post_status' => null,
'posts_per_page' => 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 );
$size = remote_file_size( $image[0], $attachment->ID );
}
}
if ( ! empty( $size ) && is_wp_error( $size ) ) {
echo '<error>' . esc_html( $size->get_error_message() ) . '</error>';
} elseif ( isset( $image[0] ) ) {
echo '<enclosure url="' . esc_url( trim( $image[0] ) ) . '" length="' . absint( trim( $size ) ) . '" type="' . esc_attr( $mime ) . '"/>';
}
}
/**
* Get the remote file size.
*
* @author Resalat Haque
* @link http://www.w3bees.com/2013/03/get-remote-file-size-using-php.html
*
* @param string $url Remote file URL.
* @param int $attachment_id Fallback in case URL is not valid.
*
* @return int File size in bytes.
*/
function remote_file_size( $url, $attachment_id = 0 ) {
// Default file size will always be 0.
$size = 0;
// Make sure URL is valid.
if ( wp_http_validate_url( $url ) ) {
// Get all header information.
$data = get_headers( $url, true );
// Look up validity.
if ( isset( $data['Content-Length'] ) ) {
// Return file size.
$size += (int) $data['Content-Length'];
return $size;
}
} elseif ( $attachment_id != 0 ) {
// If we're here, it's because we know the file exists but not the URL.
$file = get_attached_file( $attachment_id );
// Return file size.
$size += (int) filesize( $file );
return $size;
}
return new WP_Error( 'invalid_image', __( "The image URL `{$url}` was invalid or not found." ) );
}
@amrff
Copy link

amrff commented Jul 13, 2020

Hi, thanks for the code. Do you know how to make the second image in the enclosure url?

@DaveyJake
Copy link
Author

@amrff My apologies for not seeing this comment sooner. For now, this code only looks for the featured image. If you want to have all images present inside an enclosure, you'll need to use the_content filter to retrieve the image HTML from the post content. Once you have the image HTML tags, you'd then have to extract each of their src attribute values or their individual URLs. Once you have the URLs, adjust the code accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment