WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
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

share|improve this answer
is filter_ptags_on_images your solution? i just put that in my functions.php? i don't see the function in wpautop. thanks for showing me where that is regardless....good to know. :) – albert Jun 26 '12 at 0:55

have a look inside the media.php file in the wp-includes folder. In WP 3.4, around line 198. This is the function which generates the markup for images, and if I am not mistaken, you should be able to filter it in your functions.php file.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.