(07) 3040 7074

Add Images to Your WordPress Summary Feed

By: , On Monday, August 31st, 2009

wordpressThere are different reasons to use excerpts instead of full content in your rss feeds, Such as discouraging sploggers from stealing your content, and encouraging feed subscribers to visit your site. A drawback of this though is that WordPress by default only uses the first 55 words of your post in an excerpt minus any image or other media files you may have included in your post. One way around this is to enter your own custom excerpt into the excerpt field of your post editor while in HTML mode. Another method which I’m now using automatically adds the first image in your post (or a default image you specify if there is no image in your post) to your rss content, involves a function I found at Live Experience . Simply add the following code to your wp-includes/functions.php file:

function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
//Defines a default image
$first_img ="http://yoursite.com/image.png";
}
return $first_img;
}

Then open up wp-includes/feed-rss2.php or the file name that corresponds to which ever feed format you’re using, next find each instance of the following line of code

and append the following to the front of it:

' ?>

That’s all there is to it, if you’ve done everything right you should now automatically have images in your feeds, if you use a them which uses excerpts I’m sure you can think of at least one other way to use this function as well ;).

Update: The steps above involve editing some of the WordPress core files. The main problem with this is that you lose your changes whenever you update WordPress. A slightly modified and easier way is to simply place the following code into your themes functions.php file.

function get_first_image($content) {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){
//Defines a default image
$first_img ="http://yoursite.com/image.png";
}
return '
'.$content;
}
add_filter('the_excerpt_rss', 'get_first_image');
?>

If you should receive a php headers error, check to make sure there are no spaces or empty lines between any of the php code blocks in your functions.php file

Loading Facebook Comments ...

5 Responses to “Add Images to Your WordPress Summary Feed”

  1. Fernando Says:

    Hi,
    I am trying to do what is explained on the post, but it is not working. What could I do? Could you helo me, please?
    Thanks in advance,
    Fernando

    Wardell Reply:

    Hi Fernando, I’ve just realized that my blog had striped some of the characters from the function above but this has been corrected and the function should work for you now.

  2. Tevya Says:

    I tried this, but it doesn’t seem to be working. Could it be that it doesn’t work with WordPress 3? Or perhaps its the theme Suffusion? Any ideas would be helpful. The site I’m using it on is http://www.mormonlifehacker.com
    Thanks.
    Tevya´s last blog post ..Affiliate Banners- Buttons- &amp Skyscrapers

  3. Tevya Says:

    You can disregard my previous comment. It is working! Guess it just took a little while to propagate. So is there a way to just have it show the thumbnail or even small size of the image? Instead of the full size? Some images I put in posts are rather large, but I just put a small version with a link to the full size. If there were a way to just insert the thumbnail or small size, that would be much better for readers. Thanks.
    Tevya´s last blog post ..Affiliate Banners- Buttons- &amp Skyscrapers

  4. Gavin Says:

    That’s perfect, thanks a ton for posting this up and saving my morning :)

© Wardell Design 2014
Entries (RSS) and Comments (RSS).