Two methods of automatically renaming uploaded files in WordPress

When we edit and publish articles, we often upload some pictures, audio and other files. Interested webmasters will find that WordPress will not automatically rename the uploaded files. Some webmasters may be reluctant to rename the files one by one because of the large amount of files.

Then it will lead to errors such as duplicate file name and Chinese file name garbled.

There are also many online tutorials about the automatic renaming of the uploaded files of WordPress. In order to facilitate the development of topics in the future, we recommend two skills of the automatic renaming of the uploaded files of WordPress tutorials.

1、 Auto rename by upload time

//WordPressUpload file rename
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');

2、 Generate numbers with MD5 encryption and rename them

//WordPressUpload file rename
function rename_filename($filename) {
    $info = pathinfo($filename);
    $ext = emptyempty($info['extension']) ? '' : '.' . $info['extension'];
    $name = basename($filename, $ext);
    return substr(md5($name), 0, 20) . $ext;
add_filter('sanitize_file_name', 'rename_filename', 10);

usage method

Add one of the above two pieces of code to the functions.php File.

Related Article
WordPress tutorial: adding author filtering options to the background article list

When there are multiple users publishing articles in our site, in order to facilitate the administrator to view the article Read more

WordPress displays the registration time of the login user in the frontend

When using WordPress to build a website, some users want to display the user registration time in the frontend, but Read more

WordPress adds the number of SQL queries at the bottom

An excellent woodpress theme is definitely that the less SQL consulting is used, the better. How can we see how Read more

Some WordPress articles can be seen after comments / replies

If we want to hide part of the content of the article and need the user's comments and replies to Read more

Leave a Reply

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

To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image