Remove P tags from images

1) Filter wpautop() with ACF:

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');
add_filter('the_content', 'filter_ptags_on_images');

If you have both of those try checking with a later priority on the add_filter. It’s possible the theme or plugin or acf is overriding you….

add_filter('acf_the_content', 'filter_ptags_on_images', 9999);
add_filter('the_content', 'filter_ptags_on_images', 9999);

2) Edit wpautop():

<?php
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'custom_wpautop' );
function custom_wpautop() {
 // copy wpautop() code at https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-includes/formatting.php#L373 and add img to the $allblocks variable
}

3) This above is a lot of code for one task however. Consider trying this:
https://wordpress.org/plugins/preserved-html-editor-markup-plus

4) You could try this although not as good as the method you’re trying to accomplish since this one is done with javascript.

<script type="text/javascript">
jQuery(document).ready(function($){
    $('p > img').unwrap();
});
</script>

5) if it’s just the styling that’s messing things up and you don’t care about the markup:

<style type="text/css">
p > img {
    margin: 0 !important;
}
</style>

Leave a Comment