Skip to content

Instantly share code, notes, and snippets.

@duogeekdev
Last active January 23, 2017 19:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duogeekdev/b51f035d3d6927f69e48 to your computer and use it in GitHub Desktop.
Save duogeekdev/b51f035d3d6927f69e48 to your computer and use it in GitHub Desktop.
Add featured image in rss feed in a wordpress site
<?php
function add_featured_image_in_rss() {
if ( function_exists( 'get_the_image' ) && ( $featured_image = get_the_image('format=array&echo=0') ) ) {
$featured_image[0] = $featured_image['url'];
} elseif ( function_exists( 'has_post_thumbnail' ) and has_post_thumbnail() ) {
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'post-thumbnail' );
} elseif ( function_exists( 'get_post_thumbnail_src' ) ) {
$featured_image = get_post_thumbnail_src();
if ( preg_match( '|^<img src="([^"]+)"|', $featured_image[0], $m ) )
$featured_image[0] = $m[1];
} else {
$featured_image = false;
}
if ( ! empty( $featured_image ) ) {
echo "\t" . '<enclosure url="' . $featured_image[0] . '" />' . "\n";
}
}
add_action( 'rss2_item', 'add_featured_image_in_rss' );
@mathewnephews
Copy link

Hello,
I use the add_featured_image_in_rss function ...
When I use it in the function-file of my theme it works correct, but when I use it as a plugin through the mu-plugins-folder, it adds a blank line in front of the xml-declaration …
Any idea how to solve that?

Greetings!

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