If you install WordPress in a directory called wp
, the default URL for uploading images is
https://www.example.com/wp/wp-content/uploads/img.png
but you may want to change it to
https://www.example.com/assets/uploads/img.png
I will introduce two methods that are often explained in other blogs, their disadvantages, and my method to avoid those disadvantages.
Common method 1: Edit upload_path and upload_url_path
One of the most common methods is to access https://www.example.com/wp/wp-admin/options.php
and edit upload_path
to .
/assets/uploads
and upload_url_path
to https://www.example.com/assets/uploads
.
In WordPress 3.4 and earlier, these options could be set from the default admin screen, but now they are often introduced with the preliminary knowledge that they can only be set via options.php
.
However, upload_path
option disappeared from the admin screen because it was removed from WordPress 3.5, and the only reason it is still available is for compatibility.
There is no reason to recommend this method, which may not be available in the future, even though WordPress is up to version 6.
Common method 2: Rewriting WP_CONTENT_DIR, WP_CONTENT_URL
Rename and move the wp-content folder
, and add the following to wp-config.php
define('WP_CONTENT_DIR', dirname(__FILE__) . '/... /' . 'assets');
define('WP_CONTENT_URL', 'https://' . $_SERVER['SERVER_NAME'] . '/assets');
This works most of the time, but there is one pitfall.
It is that the contents of WP_CONTENT_DIR
is /path/wp/../assets
.
It should be /path/assets
, but it is a redundant notation that goes into wp
and then back out.
To avoid directory traversal (attack by putting symbols such as .
.. in the input), some plugins do not accept the notation such as ” ..
“.
Even if such plugins are not used, such paths may not be desirable.
Best Practices
So, the best practices (or what seems to be the best practices) I propose this time are as follows.
Change the code to be appended to wp-config.php
in the Common Method 2 as follows.
define('WP_CONTENT_DIR', dirname(__FILE__, 2) . '/' . 'assets');
define('WP_CONTENT_URL', 'https://' . $_SERVER['SERVER_NAME'] . '/assets');
PHP
‘s dirname
has the following API:
dirname(string $path, int $levels = 1): string
with an optional argument named $levels
.
This indicates how many levels you go up from $path
, the default value of 1
refers to the parent directory.
If you want to go back one more level from the parent directory, simply set $level=2
.
Then, the constant WP_CONTENT_DIR
will now be neatly formatted without the “..
“.