• 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)
  • Thread Starter Acts7

    (@acts7)

    I ‘m positive I need some sort of
    for each loop for the images on the_content
    but I’m awash

    I’ve also played around with this:

    preg_match_all('#align(.*?)(\b)#si', $content, $arr, PREG_PATTERN_ORDER);
    //print_r($arr);
    print_r($arr[0][0]);
    print_r($arr[1][0]);
    echo 'end of array <br/>';
    //  gets the full class name
    $alignDirctnClass = $arr[0][0]; //  output: alignleft / alignright
    //  splits off to get only the part AFTER the search phrase
    $alignDirctnOnly = $arr[1][0]; //   output: left / right

    This definitely gets me just the “left” or “right” portion I need.

    But where I’m lost is this:

    How do I compose the preg_replace to
    add the attribute of {align=$alignDirctnOnly}
    for the specific image?

    I’ve just been staring at this for too long.
    It’s actually been 3 months that I’ve been working on this.
    Pulling my hair out then walking away for a while. And trying again.

    I just can’t seem to make this final connection.
    Anyone lend a hand please?

Viewing 1 replies (of 1 total)
  • The topic ‘WP Grab image class and apply as align attribute’ is closed to new replies.