1

OK, I have looked and read quite a few message boards about this problem and I can not seem to find an answer.

I am uploading thousands of photos for a photographer and using the WP Gallery shortcode. I do not want to manually input captions for every thumbnail, I want the image title to automatically populate that area.

How do I modify the Gallery output to make this change?

edit here is a link to a gallery I have up. The image titles are currently manually input into the caption field. http://maryannerussell.com/afsp/

2 Answers 2

2

The quick & dirty way, to set the title as caption, would be to use SQL (untested):

UPDATE wp_posts 
SET post_excerpt = post_title 
WHERE  
        post_excerpt    = '' 
    AND post_type       = 'attachment' 
    AND post_status     = 'inherit'
    AND post_mime_type  = 'image/jpeg'
    AND ID              = 123

Here we target the jpeg image with ID 123 and empty caption.

Note I added the ID = 123 and post_mime_type = 'image/jpeg' as extra restrictions that you can adjust while testing. Also remember to adjust the table name of wp_posts.

WARNING: Backup your database before testing!

If you're looking for a dynamic way, then my answer here might be related.

It might also be possible for you to add the caption while uploading your images:

/**
 * Automatically set the title as caption, when uploading an attachment.
 *
 * @see https://wordpress.stackexchange.com/a/188708/26350
 */
add_filter( 'wp_insert_attachment_data', function( $data, $postarr )
{    
    // Let's target only the uploading process and not the updating of attachments:
    if( empty( $data['post_excerpt'] ) && isset( $postarr['ID'] ) && 0 == $postarr['ID'] )
        $data['post_excerpt'] = $data['post_title'];

    return $data;
}, 10, 2 );    
8
  • The quick and dirty way just saved me many hours of work tonight, thank you! It solved my problem for today. I am however looking for a dynamic way to solve the problem permanently. Ultimately I will not be uploading her image galleries any longer, someone who will not have access to running SQL will. I am hoping to find a hook or a function change that can always display the image name in place of the caption.
    – Donovan
    May 19, 2015 at 2:11
  • Great to hear that. How are you importing your images? @Donovan
    – birgire
    May 19, 2015 at 17:31
  • I am using the wordpress media import.
    – Donovan
    May 19, 2015 at 18:01
  • Please check out my updated the answer @Donovan
    – birgire
    May 20, 2015 at 12:02
  • this does add the image title as the caption but leaves the title blank, resulting in no image showing up in the gallery. postimg.org/image/hiyimfldx postimg.org/image/qs0owju9x
    – Donovan
    May 20, 2015 at 16:22
0

There is a filter called post_gallery which allows you to create your own html for your gallery. You could copy and edit the code from the gallery_shortcode function, (which creates the gallery code? and use that in the filter, with titles included/

Your Answer

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

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