Home › Forums › General Issues › Remove p around img
Hi!
I want to remove the p tag around img. Unfortunately, this code snippet doesn’t work:
remove_filter ('acf_the_content', 'wpautop');
I also tried this:
function filter_ptags_on_acf_images($acfcontent){
return preg_replace('/\s*(<a .*>)?\s*(<img .* \/>)\s*(\/a>)?\s*<\/p>/iU', '\1\2\3', $acfcontent);
}
add_filter('acf_the_content', 'filter_ptags_on_acf_images');
But that doesn’t work either.
Do I have to reload anything to make these snippets work or do I need another code to deactivate these annoying p tags??
Thanks!

This is really a general WP question. WP adds <p> tags around all images. Have you tried this https://interconnectit.com/blog/2011/06/16/how-to-remove-p-tags-from-images-in-wordpress/
If that doesn’t work I’d try some of the other results here https://www.google.com/search?q=posts_search&ie=utf-8&oe=utf-8#q=wordpress+remove+p+tags+from+images
As ACF doesn’t use the general WP the_content filter but has it’s own filter (‘acf_the_content’) I thought this is an ACF related question. The script you linked above works fine – in none ACF content fields but not in ACF. Even if I change the filter into ‘acf_the_content’ it’s not working.

It should work the same, you just need to change the hook it runs on
// img unautop
function img_unautop($pee) {
$pee = preg_replace('/<p>\\s*?(<a .*?><img.*?><\\/a>|<img.*?>)?\\s*<\\/p>/s', '<div class="figure">$1</div>', $pee);
return $pee;
}
add_filter( 'acf_the_content', 'img_unautop', 30 );
The topic ‘Remove p around img’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.