Convert WordPress Image Filename To Lowercase

Sumit Singh
Sumit Singh
21 May 2022

WordPress by default will preserve capitalization in filename for images. To make filenames lowercase automatically.

Add the following PHP code snippet in your favorite code manager plugin like Scripts organizer and set it to load on front end:

/**
 * Convert WP filename to lowercase

 * @author Sumit Singh
 * @refer https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_handle_upload_prefilter
 */
function wpl_prefix_convert_filename_to_lowercase( $file ) {

	$image_extensions = array (
		'image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'ico'
	);

	// Return if file is not an image file
	if ( ! in_array($file['type'],$image_extensions) ) {
		return $file;
	}

	$image_extension = pathinfo( $file['name'] );
	$image_name 	 = strtolower( $image_extension['filename'] );
	$file['name'] 	 = $image_name . '.' . $image_extension['extension'];
	
	return $file;
}
add_filter('wp_handle_upload_prefilter', 'wpl_prefix_convert_filename_to_lowercase', 20 );

Was this article helpful?

Tags

Categories

Never miss new post again

Subscribe and get list of new posts in your inbox

Click to Copy