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!
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 );
Now, the p tags are gone!! 🙂
Thank you very much!