/Wordpress RSS 2.0 image enclosure [COMPLETE with IMG filesize included].php
Forked from supermethod/Wordpress RSS 2.0 image enclosure
Last active Sep 7, 2020
How to add an enclosure to a wordpress RSS feed using the first image of the post [with its actual length attribute value] - add to functions.php
<?php | |
/** | |
* Wordpress RSS 2.0 image enclosure. | |
* | |
* @param WP_Query $query The current WP_Query instance. | |
* @return WP_Query | |
*/ | |
add_filter( 'pre_get_posts', 'feed_filter' ); | |
function feed_filter( $query ) { | |
if ( $query->is_feed ) { | |
add_filter( 'rss2_item', 'feed_content_filter'); | |
} | |
return $query; | |
} | |
/** | |
* How to add an enclosure to a wordpress RSS feed using the first image of the | |
* post [with its actual length attribute value]. | |
* | |
* @param mixed $item Current feed item. | |
* @return mixed Feed item. | |
*/ | |
function feed_content_filter( $item ) { | |
global $post; | |
$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 ) { | |
$image = wp_get_attachment_image_src( $attachment->ID, 'large' ); | |
$mime = get_post_mime_type( $attachment->ID ); | |
} | |
} | |
if ( $image ) { | |
echo '<enclosure url="' . $image[0] . '" length="' . remote_file_size( $image[0] ) . '" type="' . $mime . '"/>'; | |
} | |
return $item; | |
} | |
/** | |
* Get the remote file size. | |
* | |
* @see {@link http://www.w3bees.com/2013/03/get-remote-file-size-using-php.html} | |
* @author Resalat Haque | |
* | |
* @param string $url Remote file URL. | |
* @return int File size in bytes. | |
*/ | |
function remote_file_size( $url ) { | |
// Get all header information | |
$data = get_headers($url, true); | |
// Look up validity | |
if ( isset( $data['Content-Length'] ) ) { | |
// Return file size | |
return (int) $data['Content-Length']; | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
Paste inside |
This comment has been minimized.
This comment has been minimized.
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>
.
|
This comment has been minimized.
This comment has been minimized.
Hi, thanks for the code. Do you know how to make the second image in the enclosure url? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Hi. Sorry my terrible english. I past this code in main functions.php or in functions.php inside theme directory ?
thx