Envira Gallery Documentation

Documentation, Reference Materials and Tutorials for Envira Gallery

Set Captions as Image Filenames

To set image captions as their image filenames:

  1. Add the following code to a new file at wp-content/plugins/envira-set-caption-image-file.php
  2. <?php
    /**
    * Plugin Name: Envira - Set Captions to Image Filenames
    * Plugin URI: https://enviragallery.com
    * Version: 1.0
    * Author: Envira Gallery Team
    * Author URI: https://enviragallery.com
    * Description: Sets Envira Gallery Captions to their image filenames
    */
    function envira_set_captions_image_filenames( $data ) {
    	// Iterate through each gallery image
    	foreach ( $data['gallery'] as $key => $item ) {
    		$data['gallery'][ $key ]['caption'] = basename( $item['src'] );
    	}
    	return $data;
    }
    add_filter( 'envira_gallery_pre_data', 'envira_set_captions_image_filenames' );
    
  3. Navigate to the WordPress admin » Plugins and activate the Envira – Set Captions to Image Filenames plugin.

FAQs

Q: What if I want to show the filename without extension?

A: Use the following code to display the filename without extension (e.g., jpg, png, gif, etc) as the image caption:

<?php
/**
* Plugin Name: Envira Gallery - Set Captions to Image Filenames (without extension)
* Plugin URI: https://enviragallery.com
* Version: 1.1
* Author: Envira Gallery Team
* Author URI: https://enviragallery.com
* Description: Sets Envira Gallery Captions to their image filenames without the file extension
*/
function envira_set_captions_image_filenames( $data ) {
	// Iterate through each gallery image
	foreach ( $data['gallery'] as $key => $item ) {
        $filename = $item['src'];
		$data['gallery'][ $key ]['caption'] = basename( preg_replace('/\\.[^.\\s]{3,4}$/', '', $filename) );
	}
	return $data;
}
add_filter( 'envira_gallery_pre_data', 'envira_set_captions_image_filenames' );