How to Convert WordPress Uploaded Image Filenames to Lowercase

How to Convert WordPress Uploaded Image Filenames to Lowercase

Consistent file naming enhances WordPress site organization and SEO. By default, uploaded images retain original mixed case filenames.

Consistent file naming enhances WordPress site organization and SEO. By default, uploaded images retain original mixed case filenames. We walk through how to automatically convert these to lowercase for uniformity without plugins.

Follow our step-by-step approach of hooking into wp_handle_upload to catch uploads. Then use PHP’s strtolower to format filenames as lowercase before saving files.

This simple tweak tidies up media organization. More importantly, it optimizes images for SEO by matching cases with page content referring to them. Ensure your powerful visual assets don’t get undermined by improper filename cases.

Standardizing uploaded file naming requires only minor PHP edits with substantial benefits. Our tutorial aims to fix this easily overlooked issue plaguing many sites’ media consistency and WordPress SEO.

Editor's Note:

Before proceeding, ensure you have a child theme active to safely customize functionality.

Insert Custom PHP Snippet to Child Theme’s functions.php file or Code Snippet Plugin

To automatically lowercase uploaded filenames, open your child theme’s functions.php file via Dashboard’s Theme Editor or an external editor.

Add the following code snippet at the end, then save:

By loading this hook on wp_handle_upload, our custom function catches image uploads, converts names to lowercase using PHP’s strtolower, then passes execution back to WordPress with the lowercased result.

This simple tweak discretely handles converting cases for uploaded media going forward.

function explainwp_convert_uploaded_image_filenames_to_lowercase( $file ) {
	$image_extensions = array(
		'image/jpeg',
		'image/gif',
		'image/png',
		'image/bmp',
		'image/tiff',
		'ico',
	);
 
	if ( in_array( $file['type'], $image_extensions ) ) {
		$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', 'explainwp_convert_uploaded_image_filenames_to_lowercase', 20 );
Function Overview

The explainwp_convert_uploaded_image_filenames_to_lowercase function is designed to streamline image uploads in WordPress. It first determines whether an uploaded file is an image by assessing its MIME type against a set list of image formats.

Upon confirming the file as an image, it proceeds to modify the filename. This modification involves extracting the filename, transforming it to lowercase, and then reattaching the file extension.

This function effectively ensures consistency in filename formatting.

Additionally, the add_filter function integrates this custom functionality into WordPress’s upload mechanism. This integration permits the automatic modification of image filenames during the upload process, ensuring they are stored uniformly in the media library.

Save Your functions.php Modifications and Test

After you’ve modified your theme’s functions.php file, ensure to save these changes. Once saved, it’s important to carry out comprehensive testing of the new features on your WordPress website.

Test various functionalities, including user login and logout, to confirm that everything is functioning correctly. This step is crucial to ensure that the changes you’ve made are effective and work seamlessly on your site.

With these steps completed, you have effectively configured your WordPress site to automatically convert uploaded image filenames to lowercase, eliminating the need for additional plugins.

This enhancement not only ensures consistency in your media file names but also elevates the professional look of your site and supports better SEO practices.

ExplainWP
The WordPress Learning Hub

Related Posts