Skip to content

Instantly share code, notes, and snippets.

Created January 4, 2013 15:18
Show Gist options
  • Save anonymous/4453356 to your computer and use it in GitHub Desktop.
Save anonymous/4453356 to your computer and use it in GitHub Desktop.
RSS Featured Image WordPress Plugin
<?php
/*
Plugin Name: RSS Featured Image
Description: Add feature images to your blog rss.
*/
/*
* Code minimized from
* WP RSS Images (http://web-argument.com/wp-rss-images-wordpress-plugin/)
*/
function wp_rss_img_do_feed($for_comments)
{
if(!$for_comments){
add_action('rss2_ns', 'wp_rss_img_adding_yahoo_media_tag');
add_action('rss_item', 'wp_rss_img_include');
add_action('rss2_item', 'wp_rss_img_include');
}
}
function wp_rss_img_include ()
{
$image_url = wp_rss_img_url();
if (!empty($image_url)){
$uploads = wp_upload_dir();
$url = parse_url($image_url);
$path = $uploads['basedir'] . preg_replace('/.*uploads(.*)/', '${1}', $url['path']);
if (file_exists($path))
{
$filesize = filesize($path);
$url = $path;
} else {
$ary_header = get_headers($image_url, 1);
$filesize = $ary_header['Content-Length'];
$url = $image_url;
}
list($width, $height, $type, $attr) = getimagesize($url);
echo '<media:content url="'.$image_url.'" width="'.$width.'" height="'.$height.'" medium="image" type="'.image_type_to_mime_type($type).'" />';
}
}
function wp_rss_img_url($size = 'full') {
global $post;
if(function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)){
$thumbnail_id = get_post_thumbnail_id($post->ID);
if(!empty($thumbnail_id)){
$img = wp_get_attachment_image_src($thumbnail_id, $size);
}
} else {
$attachments = get_children( array(
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
'numberposts' => 1) );
if($attachments == true) {
foreach($attachments as $id => $attachment){
$img = wp_get_attachment_image_src($id, $size);
}
}
}
if (isset($img)) return $img[0];
}
function wp_rss_img_adding_yahoo_media_tag(){
echo 'xmlns:media="http://search.yahoo.com/mrss/"';
}
add_action("do_feed_rss", "wp_rss_img_do_feed", 5, 1);
add_action("do_feed_rss2", "wp_rss_img_do_feed", 5, 1);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment