Recently I’ve been play IFTTT and Buffer to try and automate some of the promotion on this here blog. One of the interesting things I’ve been trying to do is use the post’s featured image to make the tweet stand out with rich content. Unfortunately, the first try on the post I wrote last week (the one launching Taxamo for Easy Digital Downloads v1.2), lead to a bit of embarrassment with this tweeted out.
Not ideal, so I took a look as to how to add the featured image to the RSS feed.
IFTTT uses the {{EntryImageUrl}}
flag to grab an image. This is often the first image on the post, so the easiest way to make sure you add your image to your feed, before the content is displayed.
If you are using excerpt feeds, rather than full feeds, you can do this using the_excerpt_rss
filter, which is run whenever the_excerpt_rss() function is called.
From doing a bit of research, I found this post from Hugh Lashbrooke that details how to add a featured image to an RSS Feed, which I’ve edited to use the RSS excerpt, rather than checking for the feed.
To add the featured image to the WordPress RSS feed excerpt, you can use the following code, added to your theme’s functions.php or a separate plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Add the featured image to the RSS feed in excerpt * * @link https://www.winwar.co.uk/2015/02/include-wordpress-posts-featured-image-rss-feed-excerpt/?utm_source=codesnippet * * @param string $excerpt The excerpt of the feed * @return string */ function winwar_image_in_feed( $excerpt ) { global $post; if ( has_post_thumbnail( $post->ID ) ){ $image = get_the_post_thumbnail( $post->ID, 'page-thumb', array( 'style' => 'margin: 10px auto' ) ); } $excerpt = $image . $excerpt; return $excerpt; } add_filter( 'the_excerpt_rss', 'winwar_image_in_feed', 10 ); |
That will place the image in the code fine. However, should you use IFTTT as discussed at the beginning of the post, and don’t include a featured image, that can lead to situations similar to the embedded tweet above. So it’s a good idea to include a default image. To do so, modify the above code to include an “else” statement, such as this:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Add the featured image to the RSS feed in excerpt * * @link https://www.winwar.co.uk/2015/02/include-wordpress-posts-featured-image-rss-feed-excerpt/?utm_source=codesnippet * * @param string $excerpt The excerpt of the feed * @return string */ function winwar_image_in_feed( $excerpt ) { global $post; if ( has_post_thumbnail( $post->ID ) ){ $image = get_the_post_thumbnail( $post->ID, 'page-thumb', array( 'style' => 'margin: 10px auto' ) ); } else { $image = '<div style="margin: 10px auto"><img src="/* IMAGE URL HERE */"></div>'; } $excerpt = $image . $excerpt; return $excerpt; } add_filter( 'the_excerpt_rss', 'winwar_image_in_feed', 10 ); |
That will mean that should you tweet using IFTTT, you will still have images appearing. It’s a good idea to use an image such as your blog’s logo.
Finally, if you have full RSS feeds, then you will have to use the code Hugh provided in the post above. There’s also the_content_feed() filter, which could work as well, but I’ve not tested it.
Hi Rhys, thanks for the post. I’ve gone ahead and added the featured image to my RSS feed, and I’ve included the {{EntryImageUrl}} in my ifttt recipe. However, it doesn’t post the image to Twitter, it only posts a link to the image. The name of the tag: {{EntryImageUrl}} suggests that this is all it should do. Can you confirm that by using this tag, that you’re getting the image displayed as part of your tweet?
June 11, 2015 at 8:13 am