Automatically rename the WordPress Chinese name and digital name image upload code

Source: Internet
Author: User
When we use WordPress to publish articles, we often need to add images and multimedia. However, we all know that WordPress is a foreign product, but due to the mechanism of WordPress, it cannot normally display pictures or various problems. So this time, we need to bring you a comprehensive and high-quality WordPress Chinese name image Upload solution. The code and method for automatically renaming WordPress uploaded images is messy on the Internet, and is basically a static function. the compatibility and effect are not so satisfactory, for example, many of the free worpress template download zones in the first PHP community are like this, so we improved the code, this allows the WordPress media repository to upload images with Chinese names and meaningless numbers with better compatibility.

When we use WordPress to publish articles, we often need to add images and multimedia. However, we all know that WordPress is a foreign product. it is not so impressive for Chinese support and some domestic friends who prefer to use Windows Server servers, for Chinese users, we are used to naming images as Chinese. However, due to the WordPress mechanism, it is not possible to display images or various problems. Therefore, we need to provide you with a comprehensive and high-quality WordPress Chinese name image Upload solution.

Still the old rules of WordPress, add the following code to functions. php of the current topic:

// Add_filter ('sanitize _ file_name ', 'fanly _ custom_upload_name', 5, 1); function fanly_custom_upload_name ($ file) {$ info = pathinfo ($ file); $ ext = empty ($ info ['extension'])? '':'. '. $ Info ['extension']; $ name = basename ($ file, $ ext); if (preg_match ("/[one-second]/u", $ file )) {// Chinese $ file = substr (md5 ($ name), 0, 20 ). rand (00,99 ). $ ext; // Intercept the first 20 digits of MD5 length, plus two random} elseif (is_numeric ($ name) {// rename a number $ file = substr (md5 ($ name ), 0, 20 ). rand (00,99 ). $ ext; // extract the first 20 digits of MD5 length, plus two random} return $ file ;}

The above code can also solve the problem of garbled characters or non-display of attachments uploaded to the WordPress document by Chinese name, in this way, you can automatically rename and intercept images with MD5 encryption for Chinese name Images and meaningless pure numbers, which is more compatible with automatic rename of other WordPress images and has better results.

However, there are still some suggestions at the end. if you pay attention to image optimization, you may still use English words/phrases or Chinese pinyin, it is more conducive to the optimization of image naming. The above code uses the MD5 method to encrypt the name, which is not conducive to image optimization. it just seems that the desire to install and to force a higher level. Of course, adding the alt description attribute to the image is the most favorable optimization for the image.

The above is the detailed content of the automatically renamed WordPress Chinese name and digital name image upload code. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.