=== Gallery Image Captions (GIC) ===
Contributors: mlchaves
Donate link: https://ko-fi.com/marklchaves
Tags: gallery, shortcode, filter, html, css, images, captions
Requires at least: 5.3.2
Tested up to: 6.1.1
Stable tag: 1.4.0
Requires PHP: 7.2
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Gallery Image Captions (GIC) allows you to customise WordPress gallery image captions.
== Description ==
With **GIC**, you can display the title, caption, and description image attributes. You can also change/filter the rendering HTML to whatever you want.
After installing and activating GIC, write your filter and add the WordPress [Gallery shortcode](https://codex.wordpress.org/Gallery_Shortcode) to your page.
If you've been _dreaming_ of writing a filter to customise the gallery image captions, then this plugin is for you.
[Visit the live demo page.](https://streetphotography.blog/gallery-image-captions-demo/)
= Motivation =
The default WordPress gallery shortcode will only display the **caption** from the media's attachment metadata. Sometimes it's nice to display more like the title—even the description.
The **GIC plugin** overrides the WordPress gallery shortcode function to create a [hook](https://developer.wordpress.org/plugins/hooks/). With this _hook_ you can do a little bit more than just displaying the caption.
Some premium themes hide the caption completely. This leaves photography lovers like me scratching their head and spending precious time cobbling together makeshift caption blocks.
== Installation ==
1. Upload the plugin files to the `/wp-content/plugins/PLUGIN-NAME` directory, or install the plugin through the WordPress **Plugins** page directly.
1. Activate the plugin through the **Plugins** page in WordPress.
== Usage ==
= Custom Filter For Displaying Captions =
The **crux** of this plugin is the ability to filter the gallery image caption. The `galimgcaps_gallery_image_caption` hook makes this possible.
For the usage examples below, this is the filter used.
`
/**
* Custom Filter for Gallery Image Captions
*
* Note: Avoid altering captiontag, selector, and itemtag.
*/
function mlc_gallery_image_caption($attachment_id, $captiontag, $selector, $itemtag) {
$id = $attachment_id;
// Grab the meta from the GIC plugin.
$my_image_meta = galimgcaps_get_image_meta($id);
/**
* Here's where to customise the caption content.
*
* This example uses the meta title, caption, and description.
*
* You can display any value from the $my_image_meta array.
* You can add your own HTML too.
*/
return "<{$captiontag} class='wp-caption-text gallery-caption' id='{$selector}-{$id}'>" .
"Title: " . $my_image_meta['title'] . "
" .
"Caption: " . $my_image_meta['caption'] . "
".
"Description: ". $my_image_meta['description'] .
"{$captiontag}>{$itemtag}>";
}
add_filter('galimgcaps_gallery_image_caption', 'mlc_gallery_image_caption', 10, 4);
`
Feel free to use this filter code as a starter template. After activating the GIC plugin, add the code above to your child theme's `functions.php` file. Rename the function and tweak the return string to suit your needs.
= New Filter To Get Custom Fields =
`
/**
* New GIC 1.4.0 filter for custom meta fields.
*/
function gic_add_custom_fields( $image_meta, $attachment ) {
// This is how you add a custom fields to the array that
// GIC uses to display captions.
$image_meta['credit_text'] = $attachment->credit_text;
$image_meta['credit_link'] = $attachment->credit_link;
return $image_meta;
}
add_filter( 'galimgcaps_image_meta', 'gic_add_custom_fields', 10, 2 );
`
To use these two custom fields, your `galimgcaps_gallery_image_caption` would look something like this.
`
function mlc_gallery_image_caption($attachment_id, $captiontag, $selector, $itemtag) {
$id = $attachment_id;
// Grab the meta from the GIC plugin.
$my_image_meta = galimgcaps_get_image_meta($id);
// If there's credit, give it where it's due complete with link.
$credit = $my_image_meta['description'] ?
"
Credit: " . $my_image_meta['credit_text'] . "" . "
" :
'';
/**
* With GIC 1.4.0 you can also add custom media attachment fields
* to your captions.
*/
return "<{$captiontag} class='wp-caption-text gallery-caption' id='{$selector}-{$id}'>" .
"Caption: " . $my_image_meta['caption'] . "
" .
$credit .
"{$captiontag}>{$itemtag}>";
}
add_filter('galimgcaps_gallery_image_caption', 'mlc_gallery_image_caption', 10, 4);
`
**Since v1.2.0**, GIC automatically adds an **Image ID** column to your WordPress Media Library. This is to help you add the image IDs to your GIC shortcodes.
[See where GIC automatically adds an Image ID column to your WordPress Media Library.](https://ps.w.org/gallery-image-captions/assets/screenshot-11.png)
**New in v1.4.0**, GIC support custom media attachment fields.
== Usage Example 1 ==
= Shortcode =
For starters, let's use a
`