I want to be able to add a <span></span> around every image that gets posted. How will I be able to do this?

  • You could do that with Jquery. See this thread for some guidance: stackoverflow.com/questions/3987422/jquery-add-tag-a-around-img
    – Michelle
    Aug 24, 2011 at 18:18
  • do you want to do this when you save the post to the database, or when you show the post?
    – Michael
    Aug 24, 2011 at 18:18
  • I would prefer not using jQuery to achieve this... I looked into this thread, but was unable to get this to work even after editing the core files: wordpress.stackexchange.com/questions/20998/… I want this to show when it is rendered on the page. The tags are used for styling and I do not want to add the <span> tag in the html editor every time a new image is added.
    – yookd
    Aug 24, 2011 at 18:22
  • Additional clarification: When I create a new post and upload an image, a span tag should automatically be wrapped around the image. Ideally, I want to stay away from adding it in the embedded html editor.
    – yookd
    Aug 24, 2011 at 21:32
  • <span> is typically considered an inline element so you might not want to wrap all your images in such tags. If you want to achieve some specific effect, consider defining a shortcode for the purpose.
    – Raphael
    Feb 9, 2012 at 19:07

1 Answer 1


try to add a filter function to functions.php of your theme;


add_filter('the_content', 'alc_image_spanner');

  function alc_image_spanner($text) {
    $text = preg_replace_callback("#<img(.*?)>#is", create_function('$matches', 'return "<span><img".$matches[1]."></span>";'), $text);
    return $text;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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