Skip to content

Instantly share code, notes, and snippets.

@jshwlkr
Created July 11, 2022 21:10
Show Gist options
  • Save jshwlkr/f1f9dd6d1b7828f61f568bfc1cbdf558 to your computer and use it in GitHub Desktop.
Save jshwlkr/f1f9dd6d1b7828f61f568bfc1cbdf558 to your computer and use it in GitHub Desktop.
Add the featured image (and title) to an RSS feed. Note that this does not touch the atom feed. Additionally this snippet passes analysis by PHPStan.
add_action( 'rss2_item', 'prefix_add_rss_image' );
function prefix_add_rss_image() {
global $post;
if ( is_feed() && has_post_thumbnail( $post->ID ) ) {
$image_url = get_the_post_thumbnail_url( $post->ID );
if ( $image_url ) {
$image_id = get_post_thumbnail_id( $post->ID );
if ( $image_id ) {
$image_title = get_the_title( $image_id );
if ( $image_title ) {
echo '<image><title>' . sanitize_text_field( $image_title ) . '</title><url>' . esc_url( $image_url ) . '</url></image>';
} else {
echo '<image><url>' . esc_url( $image_url ) . '</url></image>';
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment