Last active
January 23, 2017 19:34
-
-
Save duogeekdev/b51f035d3d6927f69e48 to your computer and use it in GitHub Desktop.
Add featured image in rss feed in a wordpress site
This file contains hidden or 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 | |
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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!