Forked from supermethod/Wordpress RSS 2.0 image enclosure
Last active
January 12, 2024 16:28
-
-
Save DaveyJake/aa59667cd3ced5809b4d to your computer and use it in GitHub Desktop.
WP RSS 2.0 Image Enclosure with File Size
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 | |
/** | |
* 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." ) ); | |
} |
I past this code in main functions.php or in functions.php inside theme directory ?
Paste inside /your-theme-directory/functions.php
.
Ok thanks !!!
Em ter, 23 de jul de 2019 14:38, Davey Jacobson <notifications@github.com>
escreveu:
… I past this code in main functions.php or in functions.php inside theme
directory ?
Paste inside /your-theme-directory/functions.php.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/aa59667cd3ced5809b4d?email_source=notifications&email_token=AIZPCNKWABS2JUFDJWPHAXLQA46ZVA5CNFSM4IF4C662YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFVZKC#gistcomment-2978465>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AIZPCNM3ST7AJV5KABSLZBTQA46ZVANCNFSM4IF4C66Q>
.
Hi, thanks for the code. Do you know how to make the second image in the enclosure url?
@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
Hi. Sorry my terrible english. I past this code in main functions.php or in functions.php inside theme directory ?
thx