1

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', '\1\2\3', $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

0

1 Answer 1

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', '\1\2\3', $content);
    return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
}
add_filter('acf_the_content', 'filter_ptags_on_images');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.