since you need to have it for all your mail images, then you need to add a hook for the content and add
function add_responsive_class($content){ $content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"); $document = new DOMDocument(); libxml_use_internal_errors(true); $document->loadHTML(utf8_decode($content)); $imgs = $document->getElementsByTagName('img'); foreach ($imgs as $img) { $img->setAttribute('class','img-responsive'); } $html = $document->saveHTML(); return $html; }
now add content binding
add_filter ('the_content', 'add_responsive_class');
However, if you already have classes for img, and you need to add a new class, you can refer to the PHP equivalent of jQuery addClass . Or you can simply do this:
$existing_class = $img->getAttribute('class'); $img->setAttribute('class', "img-responsive $existing_class");
The above code works. I use it to remove src and data-src to lazily load the image. Hope this works for you.
AhmadAssaf
source share