Home › Forums › Back End › RSS Feed with Altered Images › Reply To: RSS Feed with Altered Images
My gut instinct would be to include width
and height
attributes on all <img>
since you can do this
img {
height: auto;
max-width: 100%;
}
to make them scale on your front end. That’s what we’re doing here: http://www.pewresearch.org/fact-tank/2014/04/03/among-afghan-public-mixed-support-for-womens-rights/
But on to your question…
Checkout the add_feed()
function (https://codex.wordpress.org/Rewrite_API/add_feed) which you would call by hooking into the init
action. It accepts two parameters: 1) the name of the feed that appears in the URL, 2) a callback function to generate the output for the feed.
function css_tricks_custom_feeds() {
add_feed( 'email', 'get_me_the_email_feed_template');
}
add_action( 'init', 'css_tricks_custom_feeds' );
For the callback function we want to do two things: 1) apply a filter on the_content_feed
, 2) include the default RSS feed template so we don’t have to fork it and we’re lazy and all that.
function get_me_the_email_feed_template() {
add_filter( 'the_content_feed', 'css_tricks_super_awesome_feed_image_magic', 10 );
include( ABSPATH . '/wp-includes/feed-rss2.php' );
}
Now we just need our function to run over the_feed_content
and manipulate the images….
function css_tricks_super_awesome_feed_image_magic( $content ) {
$doc = new DOMDocument();
$doc->LoadHTML( $content );
$images = $doc->getElementsByTagName('img');
foreach ($images as $image) {
$image->removeAttribute( 'height' );
$image->setAttribute( 'width', '320' );
}
return $doc->saveHTML();
}
I haven’t tested this. You may need to apply the css_tricks_super_awesome_feed_image_magic()
function after other filters that get applied to the_contented_feed
. To do that change the number 10 to a higher number in the add_filter()
function above.
Here’s the whole thing:
function css_tricks_custom_feeds() {
add_feed( 'email', 'get_me_the_email_feed_template');
}
add_action( 'init', 'css_tricks_custom_feeds' );
function get_me_the_email_feed_template() {
add_filter( 'the_content_feed', 'css_tricks_super_awesome_feed_image_magic' );
include( ABSPATH . '/wp-includes/feed-rss2.php' );
}
function css_tricks_super_awesome_feed_image_magic( $content ) {
$doc = new DOMDocument();
$doc->LoadHTML( $content );
$images = $doc->getElementsByTagName('img');
foreach ($images as $image) {
$image->removeAttribute( 'height' );
$image->setAttribute( 'width', '320' );
}
return $doc->saveHTML();
}