This morning just like any other, was going through my RSS feeds looking at the latest news. I had notices on sites like MacStories that they have really nice, big images in their content. I then got pulled into a rat hole of research trying to get this working on this site1.
When you insert an image in a WordPress post, you can choose a “smaller than original” image. This is great for your website, not serving the user too big of an image. But RSS readers are a little different. There isn’t nearly as much overhead to the content payload. So serving up the full image might be ok.
function rss_feed_filter($query) {
if ($query->is_feed) {
add_filter('the_content', 'rss_feed_content_filter');
}
return $query;
}
add_filter('pre_get_posts','rss_feed_filter');
function rss_feed_content_filter($content) {
// Nasty RegEx to get attributes and replace in $content.
$content = preg_replace('/<img.*src="(.*)\-[0-9]*x[0-9]*(.[a-zA-Z]{3,4})".*alt="(.*)"[ \/]?[^\>]*>/', '<img src="${1}${2}" alt="${3}" \>', $content);
return $content;
}
Thanks to Michael Martin for this post on filtering RSS feed content.
We are doing a few things:
- Getting into the RSS Feed content to filter it.
- Doing a
preg_replace() to find the
src and
alt attributes.
- With finding the
src attribute, we are only extracting the original file name and extension. We want to strip out the
-200x200 in order to get the original image size.
- Rewriting the
<img> with only the attributes we want.
…and Bob's your uncle, we have nice big images in our RSS feed!
References
↑1 | Let’s be honest, this post is also for me to test out this new code. |
---|