+91 88 00 563434 sales@webhostingpeople.net

Our Knowledge Base Articles

Company Blog

Creating a hashed upload directory structure in wordpress for sites with large amounts of files

You are here:

The issue:

A website has 2.4+ million uploads in wordpress for one month. The location was extraordinarily gradual due to so many information in a single listing. 2 million information with in wp-content/uploads/2019/07

Wanting round there was surprisingly no plugin I discovered that dealt with this in any approach. So I went to writing one thing for this consumer. So I discovered https://codex.wordpress.org/Plugin_API/Filter_Reference/upload_dir which confirmed methods to vary the add folder. I used this to create a arrange the place two random letters are made – preserving the usual add folder arrange in tact. On this case

wp-content/uploads/2019/07

turns into

wp-content/uploads/2019/07/a/b

A letter bettwen a to z is chosen at random twice to generate the brand new add folder.

To activate this plugin on the consumer website I created a file known as upload_file_hash.php beneath wp-content/mu-plugins with the under code

<?php
/*
Plugin Title: add folder hash
Description: hash add folder for great amount of information https://codex.wordpress.org/Plugin_API/Filter_Reference/upload_dir
Creator: John Quaglieri
Model: 1.0.0
Creator URI: http://webhostingpeople.internet
*/

operate upload_folder_hash( $param )
$letter = chr(rand(97,122));
$letter2 = chr(rand(97,122));
$folder = ‘/’ . $letter . ‘/’ . $letter2;
$mydir = $folder;
$param[‘path’] = $param[‘path’] . $mydir;
$param[‘url’] = $param[‘url’] . $mydir;
return $param;

add_filter(‘upload_dir’, ‘upload_folder_hash’);

As soon as performed all new uploads in media used the brand new folder construction. Downside solved.

Leave a Comment