WordPress tips: prevent uploaded images from being renamed, add timestamp
As a matter of first importance, we will momentarily figure out the most common way of transferring pictures on WordPress backend. WordPress utilizes media_handle_upload capacity to deal with the entire course of picture transferring, his means are.
use wp_handle_upload to transfer the picture to the WordPress transfer catalog.
then, at that point arrange the information and submit it to wp_insert_attachment. 3.
wp_insert_attachment work calls wp_insert_post to embed into the WordPress posts data set table.
wp_insert_post sets an exceptional post_slug utilizing the wp_unique_post_slug work prior to embeddings the picture data into the WordPress posts information base table.
The wp_unique_post_slug will go inside the information base to check if the picture name is now there, and in case it will be, it will rename the picture name, for instance, 1.jpg, it will be renamed to 1-2.jpg, if 1-2.jpg name is likewise there, it will be renamed to 1-3.jpg, etc, until the name is exceptional. This would bring about many SQL demands.
We can’t make clients demand not transferring too normal picture names like 1.jpg, yet to forestall this course of mass SQL questions, we ought to take out this issue at the source by changing the names of the pictures.
add_filter('wp_handle_upload_prefilter', function($file){
$file['name'] = time().'-'.$file['name'];
return $file;
});
This code is to add a timestamp before the picture name with the goal that the likelihood of twofold checking again can be considered as no more.
Leave a Reply