ericakfranz
2/26/2016 - 12:19 AM

Display image filename (without the extension) as the Caption.

Display image filename (without the extension) as the Caption.

<?php
/**
* Plugin Name: Envira Gallery - Set Captions to Image Filenames (without extension)
* Plugin URI: http://enviragallery.com
* Version: 1.1
* Author: Erica Franz
* Author URI: https://fatpony.me
* 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' );