WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

I would like to add the media description as a data-attribute to each image of the gallery. All the other answers favour the solution to copy the whole code from media.php to functions.php and change it there. But isn't there a more intelligent way?

It's way to much code duplication in order to change one single line of code!

share|improve this question
Have you seen this? – toscho Mar 27 '13 at 8:12
Thank you. Yes, I did. It favours the solution, to duplicate the content in functions.php. – Alexander Maiburg Mar 27 '13 at 9:16
I see no hook or set of hooks that will do what you want. I agree it would be nice, and it would be a trivial patch if you wanted to submit such a thing. Maybe it will be included in an upcoming release. – s_ha_dum Mar 27 '13 at 13:57
Thank you guys for your hints. This was exactly what I needed!! – Alexander Maiburg Mar 27 '13 at 22:42

If anybody is interested: I managed it to add a data attribute to the article thumbnail with following filter:

function post_thumbnail_add_data_attribute( $input, $post_image_id ) {
  $caption = wptexturize(get_post(get_post_thumbnail_id())->post_excerpt);
  $substitute = is_home() ? "<img" : "<img data-description=\"" . $caption . "\"";

  return str_replace("<img", $substitute, $input);

add_filter('post_thumbnail_html', 'post_thumbnail_add_data_attribute', 10, 3 );

For the gallery I followed the advice to remove the gallery shortcode...


and then to add a customized gallery function to the functions.php...

add_shortcode('gallery', 'my_gallery');

which contains the tiny customization...

// Add the caption of images in a gallery as the name attribute in order to fetch it with jQuery lightbox
$output = str_replace('<img', '<img data-description="' . wptexturize($attachment->post_excerpt) . '"', $output);
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.