Remove P tags around images in WordPress.
Here’s a WordPress tip that solves the annoyance about WordPress in the way it adds paragraph tags around images when you put ’em on posts and pages.
This can play havoc with spacing and styling and I simply can’t stand having to write extra CSS just to correct the problem.
So here’s a simple REGEX pattern that leaves the images untouched.
To employ this, you’ll have to open your Theme functions.php and add the code to the end.
The best practice approach is to add it to the functions.php file in a child theme so when the core theme gets updated, you don’t have to go and re-do the hack.
/* Remove tags that appear automatically around images in post and pages */
return preg_replace('/\s*()?\s*()\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $page_content);