jmccole83
12/12/2017 - 5:43 PM

ACF Pro - Remove p tags from images in WYSIWYG editors

Copy the following to your themes functions.php file. Ommit the opening php tag.

<?php 

// Remove <p> tags around images in editors
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', 9999);
add_filter('the_content', 'filter_ptags_on_images', 9999);