I'm currently working on a Wordpress-based new version of my website, and there is a function I'd like to add without knowing how to.
So maybe you could help me... I hope so!
I'd like to add a filter on the_content, that works for each <img ...>
within.
This function would get the width and height attributes (e.g. <img src="image.jpg" **height="120" width="300**" alt="test" />
) in variables ($thisWidth
, $thisHeight
).
Then I'd compare width & height of each image to know if it's a landscape or portait format, and add a corresponding class. Easy: if($width > $height) { ... } else { ... }
I'm not used to work with regex
and so, so this is why I need your help.
My skills allows me to write the base of the function, but no more:
function imgClass($content) {
/* for each '<img ....>' Loop ? */
$thisWidth = /* get html width attribute */;
$thisHeight = /* get html height attribute */;
if($width > $height) { /* add class='landscape' */ } else { /* add class='portrait' */ }
return $content;
}
add_filter('the_content','imgClass');
I'd be so thankful to anybody being able to help me... Thanks in advance!