Rename any uploaded file in WordPress automatically

WordPress is a very powerful blog program, we can use WordPress also build a variety of different sites, CMS, blog, etc. Recently our team built a CMS with WordPress. But every time we upload, the file name is repeated. It was overwriting the original file, if the file name is WordPress, it will be garbled. What should we do to rename any uploaded file in wordpress automatically? Then we following these small series of two methods to provide reference:

Rename any uploaded file in wordpress automatically

Rename any uploaded file in WordPress

First method

Step 1. Use the FTP tool to connect your PHP virtual host, find the root directory of the WordPress program /wp-admin/includes/file.php (you can also use the search function to search Move the file to the uploads dir)

// Move the file to the uploads dir
$new_file = $uploads['path'] . "/$filename";
if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) )
return $upload_error_handler( $file,sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );

Step 2. The above code is replaced with the code given below. (Of course, you can also add the above code comments, easy to restore later)

// Move the file to the uploads dir
$new_file = $uploads['path'] . "/".date("YmdHis").floor(microtime()*1000).".".$ext;
if ( false === @ move_uploaded_file( $file['tmp_name'], $new_file ) )
return $upload_error_handler
( $file, sprintf( __('The uploaded file could not be moved to %s.' ), $uploads['path'] ) );

Step 3. save file.php so that you can upload pictures automatically rename the WordPress.

The above code means that after saving the original file is overwritten, then upload the file will be renamed to ”Year, Month, Day, hour, minute, seconds integer ” format like "2008-03-13 07:12:35" This will no longer have to worry about the same picture file name and overwrite the original file.

Second method

Step 1. In our wordpress website template directory, under the(wp-content\themes\theme file name\) functions.php add the following code. Put directly in functions at the end of the PHP file, as shown below.

function new_filename($filename) {
$info = pathinfo($filename);
$ext = empty($info['extension']) ? ” : ‘.’ . $info['extension'];
$name = basename($filename, $ext);
return substr(md5($name), 0, 20) . $ext;
add_filter(‘sanitize_file_name’, ‘new_filename’, 10);

Step 2. Save functions.php and you are ready to rock.

The above code will automatically rename the file name in the upload process. Name rules for the system automatically generate a 32-bit MD5 encrypted file name. (Because the 32-bit filename is a bit long, we truncated it to 20 bits in substr(md5($name), 0, 20).

Also read: Top 10 Things To Do After Installing WordPress

If you liked this article, then please follow us on social media and don’t forget to Subscribe to our mail list.

You May Also Like

About the Author: WPC Staff

WPCrons staff has long-term experience of WordPress & like to constantly spot problems and plotting how to solve them. We believe you don't need to be a nerd or a programmer or a network engineer to make a difference.

Leave a Reply

Thanks for choosing to leave a reply. Your opinions and comments are very important to us, and your email address will NOT be published. If you need a private conversation then use our contact form. Please add an avatar if you do not have and make the comment section more beautiful.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. Accept! No, thanks!

Why my browser don’t show me the coupon?

By default, Google Chrome and some other browser block pop-ups from automatically showing up on your screen. When a pop-up is blocked, the address bar will be marked Pop-up blocked Pop-up blocked.

ComputerAndroidiPhone & iPad

  1. On your computer, open Chrome.
  2. At the top right, click More More and then Settings.
  3. Under “Privacy and security,” click Site settings.
  4. Click Pop-ups and redirects.
  5. At the top, turn the setting to Allowed or Blocked.

  1. On your Android phone or tablet, open the Chrome app Chrome.
  2. To the right of the address bar, tap More More Settings.
  3. Tap Site settings and then Pop-ups and redirects.
  4. Turn Pop-ups and redirects on or off.

  1. On your iPhone or iPad, open the Chrome app Chrome.
  2. Tap More More and then Settings Settings.
  3. Tap Content Settings and then Block Pop-ups.
  4. Turn Block Pop-ups on or off.

Share via


Subscribe to get FREE updates

Join 1000s of readers around the globe. Don’t worry. We also don’t like Spam. We are weekly.


We are using affiliate links & images from respective product sites in our articles occasionally, means that if you click on one of the links and purchase an item, we may receive a commission (at no additional cost to you). All the reviews & opinions (positive or negative) are 100% our own. We are not getting any money to write them. The trademarks mentioned in this website belong to the respective companies. All the articles are information purpose only, to help someone to educate & save money. In case any problem with the content, you can reach us anytime through our contact us page »