When it comes to naming WordPress image uploads, I personally prefer renaming them with a consistent convention for easier migration and management later. My blog currently uses a “manual intelligence” approach — I rename each image to match its article’s English title. But for sites with large volumes of images, this is far too tedious. So here’s a WordPress tip that lets WordPress automatically rename uploaded images based on the date and time.
Here’s the code:
/*
Plugin Name: WordPress Auto-Rename Uploaded Images
Plugin URI: https://imzl.com/wordpress-upload-pictures-renamed.html
Description: WordPress Tips: Auto-rename uploaded images by date and time
Version: 0.1
Author: Len Chou
Author URI: https://imzl.com/
*/
function huilang_wp_handle_upload_prefilter($file) {
$time = date("Y-m-d");
$file['name'] = $time . mt_rand(1, 100) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);
return $file;
}
add_filter('wp_handle_upload_prefilter', 'huilang_wp_handle_upload_prefilter');
Usage
Save this code as a .php file, upload it to the wp-content/plugins directory, then activate the plugin from the admin panel. If you prefer not to use it as a plugin, paste the code into your current WordPress theme’s functions.php (when pasting into functions.php, make sure to remove the opening <?php on the first line, otherwise your site will crash).
The code above renames images using a “year-month-day + random number” format. If you want to include hours, minutes, and seconds, change line 12 from
$time=date("Y-m-d"); to $time=date("Y-m-d H:i:s");.