Envira Gallery Documentation

Documentation, Reference Materials and Tutorials for Envira Gallery

How to Set Captions as Image Filenames

Would you like to automatically set captions as image filenames? With Envira you can do this simply by creating a custom plugin. We’ll walk you through the steps on how to set captions as image filenames automatically.

This tutorial is a bit more technical than our other docs, but we’ll walk you through the process step by step. In order to set captions as image filenames automatically, we’ll need to create and upload a basic WordPress plugin.

To automatically set captions as image filenames, just add the following code to a new file at wp-content/plugins/envira-set-caption-image-file.php.

<?php
/**
* Plugin Name: Envira Gallery - Set Captions to Image Filenames
* 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' );
The referenced code above will automatically set your filename as the image caption but remove the file extension.

Not sure how to do that? Just follow these steps:

  1. Open a text file and make sure that it is a plain text document. You can use a plain text editor like Notepad or a code editor of your choice.
  2. Next, copy and paste the code above into the file and save the file as envira-set-caption-image-file.php
  3. Once you have saved the file you can easily upload this directly to your /plugins directory on your server using FTP or you can right-click on the text document and zip (or compress).
  4. Finally, log in to your WordPress dashboard and go to Plugins » Add New » Upload Plugin and upload the .zip file you just created in the previous step.

Your next step is to activate the plugin you just uploaded. Simply navigate to the Plugins from within your WordPress dashboard and activate the Envira Gallery – Set Captions to Image Filenames plugin.

Activate the custom plugin to set captions as image filename for your gallery images


A: Envira won’t update your database entry for the caption but when you enable captions to show under the image or in the lightbox, this plugin will automatically pull that filename for your image caption.

[i]
[i]