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

So, below is the Wordpress default generated code for a gallery item:

<div id='gallery-1' class='gallery galleryid-203 gallery-columns-3 gallery-size-thumbnail'>
    <dl class='gallery-item'>
        <dt class='gallery-icon landscape'>
                <a href='image url'><img width="150" height="120" src="thumbnail image" class="attachment-thumbnail"/></a>
        <dd class='wp-caption-text gallery-caption'>Caption Text</dd>

What I need, is to surround the caption text with the image url, so when the user clicks on the caption it will open the big image, like this:

<dd class='wp-caption-text gallery-caption'><a href="image url">Caption Text</a></dd>

I know I can do this via image gallery with a href in the caption text, but the site is for a client and I want to be done dynamically.

I also found that this can be done in media.php (via functions.php), but I don't know how...

I hope I've made myself clear :)

share|improve this question

That markup is created by the caption shortcode. If you look at the the source for that shortcode you will see this:

 // Allow plugins/themes to override the default caption template.
 $output = apply_filters('img_caption_shortcode', '', $attr, $content);
 if ( $output != '' )
  return $output;

That means that you can hook a function into img_caption_shortcode and completely hijack the caption shortcode. So copy the code from line ~643 to the end of the function, insert that into your own function, hook that to img_caption_shortcode, and make whatever changes you need to get the markup you want.

share|improve this answer
Thank's for the answer, but I was thinking that I should alter this code: return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">' 656 . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>'; – Cristian T Sep 6 '13 at 14:27
Yes, you should alter that code, but you should do so the way I explained and not by hacking Core files. – s_ha_dum Sep 6 '13 at 14:37
Ok, as you see I know where to modify, but the problem is I don't know how, could you please provide me the code? Of course I will not alter the media.php, I will insert the code into functions.php. thank you. – Cristian T Sep 6 '13 at 14:59
I will see what I can do a little later – s_ha_dum Sep 6 '13 at 15:08

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.