Forked from DaveyJake/wp-rss2-image-enclosure.php
Last active
February 27, 2020 10:35
-
-
Save tombennet/4c4e81862c7f02dfd46e81d2ff7e202c to your computer and use it in GitHub Desktop.
How to add an enclosure to a WordPress RSS feed using the featured image of the post - 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 | |
/** | |
* 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 featured 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; | |
if(!has_post_thumbnail($post->ID)) return; | |
$thumbnail_id = get_post_thumbnail_id( $post->ID ); | |
if(empty($thumbnail_id)) return; | |
$image_full = wp_get_attachment_image_src( $thumbnail_id, 'full' ); | |
$image_medium = wp_get_attachment_image_src( $thumbnail_id, 'medium' ); | |
$image_thumb = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail' ); | |
$image_mime = get_post_mime_type($thumbnail_id); | |
echo '<enclosure url="' . $image_thumb[0] . '" length="' . filesize( $image_thumb[0] ) . '" type="' . $image_mime . '"/>'; | |
return $item; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment