🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

Several Methods to Optimize WordPress Image Upload Speed and Reduce Database Queries

Hello everyone, I am Brother Leifeng. Today, I will introduce some methods to optimize image uploads in the WordPress backend. Previously, when uploading images in the WordPress backend, Brother Leifeng sometimes encountered errors, especially when uploading multiple images at once, which could easily cause high database load. Below, I will explain how to optimize WordPress image upload speed and reduce database queries.

Do not name WordPress uploaded images as '1', otherwise it will increase database queries:

Some people are too lazy to name images and just use something like 1.jpg. When uploading such an image in the WordPress backend, WordPress will check if 1.jpg already exists, then automatically rename it to 1-2.jpg. If 1-2.jpg also exists, it continues to automatically rename, cycling through this process, putting immense pressure on database queries. If you are truly lazy, you can refer to the method below.

Automatic Renaming of Chinese Named Images in WordPress / Reduce SQL Queries:

Situation 1: If you are on a Windows server, uploading images with Chinese names will result in an error, prompting„An error occurred during upload. Please try again later.“Although plugins can be used to automatically convert names to pinyin/English, this can affect performance during image upload. Here, a piece of code can be used to automatically rename images upon upload. Situation 2: If there are too many duplicate image names in WordPress uploads, WordPress needs to query the database and then rename the images. With too many images, this could even cause the database to crash. The solution is simple: add the following code to functions.php:
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ){
    $info = pathinfo($file['name']);
    $ext = $info['extension'];
    $filedate = date('YmdHis').rand(10,99);
    $file['name'] = $filedate.'.'.$ext;
    return $file;
}
The principle is to rename images based on the upload time, for example: 2019092010182810.jpeg. If you don't know how to add code, you can refer to:Safe Method to Add Code to the functions.php File: Code SnippetsOf course, you can also directly use this plugin:WordPress Image Auto-Rename Plugin Unique Rename Image File Upload

Automatic Compression of JPG Format Images Uploaded to WordPress:

Compressing images can save bandwidth and speed up website loading. WordPress provides image compression functionality by default. We can set the compression ratio. Note that this only supports JPEG format images. Add the following code to functions.php:
add_filter( 'jpeg_quality', create_function( '', 'return 90;' ) );
The 90 in the code is the compression ratio; adjust it according to your needs. For other image compression requirements, you can use the 「EWWW Image Optimizer「 plugin, which supports JPG, PNG, and even GIF. It also provides the functionality to convert image formats during compression, such as converting to WebP format, as well as converting between JPG and PNG.There are many similar image compression plugins; you can choose one that you find convenient to install and use.

Reduce Thumbnails for WordPress Uploaded Images:

When uploading images in WordPress, three sizes of thumbnails are automatically generated, which can somewhat affect the performance of virtual hosts/small-memory VPS. We can disable this or only generate one size of thumbnail. In the WordPress admin settings → Media → Image sizes, set the thumbnail sizes you don't need to generate to 0.If you really don't need thumbnails, then disable them completely. Add the following code to functions.php:
//彻底禁止WordPress缩略图
add_filter( 'add_image_size', create_function( '', 'return 1;' ) );
Related articles:Disable WordPress 5.2.1 automatic thumbnail cropping feature

Clean Up Unused WordPress Thumbnails:

As mentioned above about disabling thumbnails, the previously generated thumbnail images take up a lot of space. Manually cleaning them up would be overwhelming, so let「s use a plugin. 」Delete not used image「 is a plugin that can automatically search for unused thumbnails. 1. Before using Delete not used image, it will prompt you to back up first (it」s recommended to back up). You can back up in the 「options」 under Backup system / Create backup folder, or you can back up using other methods. 2. In the 「options」 under _ Show, set original, then simply click to ensure the original images are not lost. 3. Click 「Images」 to start searching for thumbnail images, then 「Delete all」 to delete them all.If you have too many thumbnail files, it may take a long time and stay in the "Fetching server…" state; please wait patiently. With the optimization settings above, you'll find that uploading images in the backend is much faster. Now you can confidently upload images without worrying about excessive database queries causing the site to crash.Related Articles: What to do if website images load slowly_3 methods for website image acceleration How to count the number, formats, and sizes of images on a WordPress website Modify WordPress website login interface LOGO and images without code Automatically Add ALT and Title Names to Images in WordPress

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step.

「Naibabiji WordPress Website Building Coaching Service」—From choosing a domain and buying hosting, to installing a Theme and publishing content, I「ll coach you through every step, helping you avoid detours and reach your goal directly.

👉 Learn about the Website Building Coaching Service
🔒

Comments are closed

The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.

×
二维码

Scan QR Code to Follow

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: