Automatically rename uploaded images in WordPress and rename images by time

Select the theme editor in the appearance options of the WordPress backend, and insert the following code into the theme's functions.php template file. It is simple and convenient. After saving the file, you can automatically rename the file when you upload it in the media.

Select the theme editor in the appearance options of the WordPress background, select the init-functions.php template file in the theme functions directory and insert the following code. It's simple and convenient!

Rename pictures by time
When uploading a file, it will be renamed in the format of "year-month-day-hour-minute-second + thousandths of milliseconds integer", such as "20200307031021765.jpg"
[cc lang="php"]
// Rename the uploaded file
function git_upload_filter($file) {
$time = date("YmdHis");
$file['name'] = $time . "" . mt_rand(1, 100) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);
return $file;
}
add_filter('wp_handle_upload_prefilter', 'git_upload_filter');

After saving the file, you can automatically rename it when you upload it in Media.

score

Leave a Reply

Your email address will not be published. Required fields are marked *