Skip to content

Instantly share code, notes, and snippets.

@tomharrigan
Created June 10, 2015 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomharrigan/707c0c264aaf2feda07d to your computer and use it in GitHub Desktop.
Save tomharrigan/707c0c264aaf2feda07d to your computer and use it in GitHub Desktop.
Add image to RSS feed
/**
* Add namespace for the featured image to RSS feed
*
* @return string
*/
function rss_img_adding_yahoo_media_tag(){
echo 'xmlns:media="http://search.yahoo.com/mrss/"';
}
add_action( 'rss_ns', 'rss_img_adding_yahoo_media_tag' );
add_action( 'rss2_ns', 'rss_img_adding_yahoo_media_tag' );
/**
* Add featured image to RSS feed
*
* @return string
*/
function rss_img_include() {
global $post;
if ( $thumbnail_id = get_post_thumbnail_id( $post->ID ) ) {
$img = wp_get_attachment_image_src( $thumbnail_id, 'full' );
if ( !empty( $img ) ) {
$url = parse_url( $img[0] );
$path = ABSPATH . ltrim( $url['path'], '/' );
if ( file_exists( $path ) ) {
echo '<media:content url="' . esc_url( $img[0] ) . '" width="' . esc_attr( $img[1] ) . '" height="' . esc_attr( $img[2] ) . '" medium="image" type="'. esc_attr( get_post_mime_type( $thumbnail_id ) ) . '" />';
}
}
}
}
add_action( 'rss_item', 'rss_img_include' );
add_action( 'rss2_item', 'rss_img_include' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment