How to remove p tags around img and iframe tags in the acf wysiwyg field

I am querying an advanced custom fields wysiwyg field. Problem is in the output wordpress wraps occasional imgs and iframes with p tags no matter what i try. Deactivate the wpautop works remove_filter ('acf_the_content', 'wpautop');but if i try to exclude only imgs and iframes with the following snippet it fails:

function filter_ptags_on_images($content)
{
    $content = preg_replace('/<p>s*(<a .*>)?s*(<img .* />)s*(</a>)?s*</p>/iU', '123', $content);
    return preg_replace('/<p>s*(<iframe .*>*.</iframe>)s*</p>/iU', '1', $content);
}
add_filter('the_content', 'filter_ptags_on_images');

Does anyone have a suggestion how to accomplish that goal? Best regards Ralf

Related posts

1 comment

  1. Ok if you want to strip the p tags for img’s and iframe’s for advanced custom fields you have to exchange the_content with acf_the_content . The code looks that way:

    function filter_ptags_on_images($content)
    {
        $content = preg_replace('/<p>s*(<a .*>)?s*(<img .* />)s*(</a>)?s*</p>/iU', '123', $content);
        return preg_replace('/<p>s*(<iframe .*>*.</iframe>)s*</p>/iU', '1', $content);
    }
    add_filter('acf_the_content', 'filter_ptags_on_images');
    

Comments are closed.