Quantcast
Viewing latest article 11
Browse Latest Browse All 34

Resize img tags in a Wordpress feed

I would like to update the_content_rss in WordPress so it would fit feedburner. It would need to find every img tag, see if it has a width and height set. If they are larger than 600 they should be reduced to 600. If they are not set, then width should be set to 600. I thought of using some of the code here to do it, but I got a bit stuck, I would appreciate help with fixing it.

Questions:

  1. Does it work?
  2. How can it find if width is null - and in which case to add it?

    <?phpfunction feedburner_img_resize($the_content) {// Create a new istance of DOMDocument$post = new DOMDocument();// Load $the_content as HTML$post->loadHTML($the_content);// Look up for all the <img> tags.$imgs = $post->getElementsByTagName('img');// Iteration timeforeach( $imgs as $img ) {        // if width is smaller than 600 - no need to continue    $width = $img->getAttribute('width');    if( $width < 600 ) continue;    $img->removeAttribute('width');    $img->setAttribute('width', 600);    $img->removeAttribute('height'); // so the image is not distorted}; return $post->saveHTML();}add_filter('the_content_rss', 'feedburner_img_resize');?>

Viewing latest article 11
Browse Latest Browse All 34

Trending Articles