WP Grab image class and apply as align attribute
-
I’m trying to hard-code the align attribute based on the class chosen in the image media button.
I can’t do it using the DomDocument method as I’m trying to use this in mailchimp and it doesn’t see jquery or anything post DOM rendering
I start with this<p>I am copy here just for fun but please dont remove me</p> <img src="xxx" class="alignleft class2" alt="xxx" /> <img src="xxx" class="alignright class3 class4" /> <p>more copy but please dont remove me</p> <img src="xxx" class="alignnone class93 class11" title="xxx" /> <img src="xxx" class="class2" alt="xxx" />
and would like to end up with this
<p>I am copy here just for fun but please dont remove me</p> <img src="xxx" class="alignleft class2" alt="xxx" ALIGN="LEFT" /> <img src="xxx" class="alignright class3 class4" ALIGN="RIGHT" /> <p>more copy but please dont remove me</p> <img src="xxx" class="alignnone class93 class11" title="xxx" ALIGN="NONE" /> <img src="xxx" class="class2" alt="xxx" /> {{{this one didn't have anything to change}}}
Here’s what I have so far. I’m not sure whether my regex is wrong or if its my preg_replace_callback script
function set_image_attribute_from_class($content){ // need to look only at the images, pref only those with class beginning "align" // ex: <img class="alignleft class2" /> or <img class="alignright class3 class4" /> $search = "/(<img\b src=)"([^"]+)"(.* class=".*align(?:.|[^"]*)"[^>]+>)/"; $html = preg_replace_callback($search, 'my_replace_callback', $content); return $html } function my_replace_callback($match) { preg_match_all('#align(.*?)(\b)#si', $match, $arr, PREG_PATTERN_ORDER); $eachImagePartialCssClassName = $arr[1][0]; //the css after the word "align" return {{{original image with classes intact}}} . 'align='. $eachImagePartialCssClassName .'/>'{{{close the image tag}}}' }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘WP Grab image class and apply as align attribute’ is closed to new replies.