Support » Plugins » Hacks » Display Title as the Caption – wordpress gallery

  • I have quite a few images (over 200) that I will be uploading and their filenames are quite descriptive and would work well for captioning. However, I’d like to automate the caption process (instead of typing in 200+ captions individually). I searched the forums and found this thread ( ) that appeared to have a solution – however, when I added it to my functions.php file, it did not work (didn’t break anything, but didn’t actually populate the caption field with the title). Perhaps I misunderstood the functionality it was presenting? Not sure. If anyone can point me in the right direction I’d really appreciate it. Thank you so much!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Just realized that thread I referenced had solution for pulling the POST title in the caption – not the IMAGE title. That is *not* what I want to do, lol….Again, any suggestions are appreciated.

    Moderator bcworkz


    I’m guessing that solution works for galleries and images attached in the same way, not necessarily any embedded image. The image url is available within that hook, so something similar would be an elegant solution if you are able to attach the images the same way.

    Left to my own devices, I probably would hook ‘wp_insert_post_empty_content’, search the content for bare image urls, unassociated with captions, then extract needed info from the img tag and then wrap the img tag in a caption shortcode.

    When creating a post, you just insert the plain image, when the post is saved, the caption would be added by script. To prevent the script from doing this, you would have to manually add a caption, even if it were a just space character.

    Not nearly as elegant, but it should work. The best approach probably depends on how you plan to add images.

    The code in this post worked:

    However, realize now that I have *other* images in galleries where I already have captions set up and need those to still display….

    So, any suggestions from anyone on how to modify this section of code:

    if ( $captiontag && trim($attachment->post_title) ) {
    					$output .= "
    						<{$captiontag} class='wp-caption-text gallery-caption'>
    						" . wptexturize($attachment->post_title) . "

    to only display the title IF there is no caption (i.e., if the caption field is blank)?


    Moderator bcworkz


    Totally untested and a guess on top of it, but first line maybe could be:
    if ( $captiontag && !$attachment->post_excerpt) ) {

    Assuming the caption is stored in post_excerpt for image attachments, otherwise whatever variable has it should be used. I’m also assuming there will always be a post title, so you should add that check back in if not true. May also need a more stringent test for lack of caption than a simple NOT operator. But something along those lines.

    Since you now have full control of the gallery code, surely you can do most anything you desire.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display Title as the Caption – wordpress gallery’ is closed to new replies.