Created
June 10, 2015 23:08
-
-
Save tomharrigan/707c0c264aaf2feda07d to your computer and use it in GitHub Desktop.
Add image to RSS feed
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
/** | |
* 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