How do I remove generated HTML around images in posts?

When I insert images into a blog post, wp is automatically inserted markup around the img element: specifically a div and a p (for the caption)…..where is that markup being generated in wp sourcecode?

2 Answers
2

function filter_ptags_on_images($content){
   return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}

add_filter('the_content', 'filter_ptags_on_images');

The p tags come from wpautop

Leave a Comment