Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marklchaves/1116bba6fcd9688b4df5b121d5d76277 to your computer and use it in GitHub Desktop.
Save marklchaves/1116bba6fcd9688b4df5b121d5d76277 to your computer and use it in GitHub Desktop.
Use the WordPress Media Title and/or Caption Fields for Gallery Image Captions
<?php
/**
* Custom Gallery Image Caption Filter
*
* I want to use any of the media
* attachment meta that's available. In
* this example, we'll use the image title
* and caption values for the gallery caption.
*
* $id is the attachment ID (i.e., media file).
*
* ~mlc 16 February 2020
*/
$filtered_caption =
apply_filters( 'gallery_image_caption', $id, $captiontag, $selector, $itemtag );
// Custom: If filtered then use the new content.
if ( ! empty( $filtered_caption ) ) {
$output .= $filtered_caption;
// Custom: Else use the default WordPress caption logic.
} else {
if ( $captiontag && trim( $attachment->post_excerpt ) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'>" . wptexturize( $attachment->post_excerpt ) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
} // Custom: else
@marklchaves
Copy link
Author

Now Available as a Plugin!

I'm excited to announce the release of the official Gallery Image Captions (GIC) plugin!

GIC is a minimalist low footprint plugin that created the gallery shortcut override for you. All you have to do after activating GIC is write your filter.

If you decide to use the GIC plugin, I'd appreciate your feedback. Either post to the support forum or write a review.

GIC Logo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment