Move WordPress Uploads Out of WordPress Directory

Here’s a simple one (which took me many hours to correctly configure). This little script, inserted into a custom plugin or functions.php in a child-theme will allow you to move your uploads folder out of the WordPress directory. Keeps things neat and tidy for my purposes.

Currently I have wordpress in its own directory, ie. website.com/wordpress/

Now assume publicly facing url is website.com, that makes my uploads website.com/wordpress/wp-content/uploads – a terribly unprofessional looking URL.

With this code you can move the uploads directory to the top folder. Be sure to move the files, or use Better Search and Replace to change your DB if required.

add_filter(
'upload_dir',
function ($dirs) {
$new_upload_dir = '/home/domains/website.com/public_html/uploads';
$new_baseurl = 'https://website.com/uploads';
$dirs['path'] = $new_upload_dir . $dirs['subdir'];
$dirs['basedir'] = $new_upload_dir;
$dirs['baseurl'] = $new_baseurl;
$dirs['url'] = $new_baseurl;
return $dirs;
},
15 // default is usually 10
);

Note: you’ll need to work out where ‘$new_upload_dir’ with some detective work of your own. Good luck!

Leave a Reply