Jeremy Knauff

Add Featured Image to WordPress RSS with functions.php

May 22nd, 2013 by Jeremy Knauff

RSS Feed

I was configuring an Aweber email list to create and send a newsletter automatically from a WordPress RSS feed each time a new post was published. The problem is that while the images within a post are included in a full RSS feed, the featured image is not. There are plugins that can add this functionality, but I avoid using plugins whenever possible to keep my clients’ websites lean and running fast, so I whipped up this PHP code that you can simply paste into your functions.php file to accomplish the task.

This script determines the featured image for each post, wraps a link to the post around it, applies some CSS styling, and then drops it into your RSS feed. The result is a clean and simple way to put your featured image into your RSS feed and increase click-through rates to your website.


//Featured image in RSS
//PHP code created by Jeremy Knauff, of Designed by JK

function featured_image_RSS( $content ) {
    global $post;
    if( is_feed() ) {
        if ( has_post_thumbnail( $post->ID ) ){
            $output = '<a href="' . get_permalink( $post->ID ) . '">' . get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float: left; margin: 6px 10px 0 0; border: 1px solid #957643; padding: 3px;' ) ) . '</a>';
            $content = $output . $content;
        }
    }
    return $content;
}
add_filter( 'the_content', 'featured_image_RSS' );

Enjoy!

Tags: , , , , ,

Leave a Reply

Email Marketing $19/Month!

Recent Work

USA Mobile Drug TestingGeneral Fire EquipmentKey West Home Theater

Copyright 2014 Design by JK, All Rights Reserved. | 5 Reasons You Shouldn't Copy My Stuff.