ACF WordPress: How to check for images in specific WYSIWYG fields without duplication

I have two custom fields in ACF for WYSIWYG content, named left_content and right_content. My goal is to verify if either of these fields has images and then to apply a CSS class called image_present if any do.

The issue I’m facing is that my current implementation detects images across both fields. For instance, if I upload an image exclusively to the right content field, it still appears in both the left and right columns on the website, creating duplicates.

Here’s the code I am using:

if(get_sub_field('left_content')){
    if(get_sub_field('left_content', $attachment['ID'])) {
        $left_area.= '<div class="column left_area image_present">'.wpautop(apply_filters('the_content',get_sub_field('left_content'))).'</div>';
    } else {
        $left_area.= '<div class="column left_area">'.wpautop(apply_filters('the_content',get_sub_field('left_content'))).'</div>';
    }
}

if(get_sub_field('right_content')){
    if(get_sub_field('right_content', $attachment['ID'])) {
        $right_area.= '<div class="column right_area image_present">'.wpautop(apply_filters('the_content',get_sub_field('right_content'))).'</div>';
    } else {
        $right_area.= '<div class="column right_area">'.wpautop(apply_filters('the_content',get_sub_field('right_content'))).'</div>';
    }
}

I had hoped that this would isolate the image detection for each field, but it’s currently not functioning as expected. When I add images to the right content field only, both columns end up displaying the same images. How can I ensure that the image detection only pertains to the specific field where the image is uploaded?

Your conditional logic is broken. That get_sub_field('left_content', $attachment['ID']) syntax makes no sense for checking images in WYSIWYG fields. You’re trying to use an attachment ID as the second parameter, but that’s not how ACF works. I’ve hit this same issue before - parsing the HTML content directly works way better. Try preg_match('/<img[^>]+>/i', get_sub_field('left_content')) to check for image tags in each field. This checks the actual HTML output from the WYSIWYG editor instead of trying to reference attachment IDs that don’t match your field structure. The regex only returns true if images exist in that specific field’s content, so you won’t get that cross-contamination between your left and right columns.

Your issue lies in the use of get_sub_field() with $attachment['ID'] in your conditionals. Instead of using that as a check for the presence of images in the WYSIWYG fields, consider looking for <img> tags directly in the content. You could employ strpos() or preg_match() to determine if images are present. For example, using strpos(get_sub_field('left_content'), '<img') !== false will effectively identify images in the specific field. This way, each field is independently assessed for image content, eliminating the duplication you are experiencing, as the logic should focus solely on the content of each field.

yeah, $attachment['ID'] seems off. maybe use has_shortcode() to spot images in the WYSIWYG content instead, or search for <img> tags with stripos(). for example, stripos(get_sub_field('left_content'), '<img') !== false would be better for checking images in each field individually.