Opened 7 years ago
Last modified 2 months ago
#45725 new enhancement
Unable to use the UPLOADS constant with WordPress in a different directory
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | Upload | Keywords: | has-patch needs-docs 2nd-opinion reporter-feedback |
| Focuses: | Cc: |
Description
The problem
When WordPress is installed in a different directory (you can achieve that by following these instructions), the UPLOADS constant is unable to function correctly in some cases according to the wp_upload_dir()'s output. Occasionally the constant accepts a relative path what will be appended to the ABSPATH constant to determine the basedir and to the site_url() function to determine the baseurl for the uploads location.
Although WordPress does let you move the CMS (it actually can be anywhere on the filesystem), however the uploads directory will always be relative to the CMS directory (ABSPATH constant) when using the UPLOADS constant.
The use case
There are multiple use cases which will be affected by this but let's consider the next few parameters:
- Website URL: example.com
- Website DIR: /foo/bar
- WordPress URL: example.com/wordpress
- WordPress DIR: /foo/bar/wordpress
Our goal is to store uploads at:
- Uploads URL: example.com/uploads
- Uploads DIR: /foo/bar/uploads
However when we defining the UPLOADS constant as 'uploads', will result in the following:
- Uploads URL: example.com/wordpress/uploads
- Uploads DIR: /foo/bar/wordpress/uploads
You might wonder what will happen when we use an absolute value for the constant instead, in this case '/foo/bar/uploads' is used:
- Uploads URL: example.com/wordpressfoo/bar/uploads
- Uploads DIR: /foo/bar/wordpressfoo/bar/uploads
The solution
Possible solutions where I could came up with are, the two following:
- Add another constant like ABSPATH to the index.php, this could be tricky for some people to update but the benefits of it are very useful. It will allow you to use one WordPress installation for all your WordPress websites. How you might wonder? This is how, I've been using this already for years!
- Another solution could be to introduce a new constant specifically for the uploads directory path and only use the current UPLOADS constant for the url.
Both of these solutions require to be implemented into the _wp_upload_dir() function on line 1972 in wp-includes/functions.php
Have a look at the patch attached to this ticket, with the patch WordPress will introduce both the UPLOADS_DIR and INDEX_ABSPATH constant. According to some tests I did it should also be backward compatible.
@Fleuv you're correct about the limitation.
UPLOADSis concatenated with ABSPATH (line 2461 according to the latesttrunk)/wp-includes/functions.php/* * Honor the value of UPLOADS. This happens as long as ms-files rewriting is disabled. * We also sometimes obey UPLOADS when rewriting is enabled -- see the next block. */ if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) { $dir = ABSPATH . UPLOADS; $url = trailingslashit( $siteurl ) . UPLOADS; }However, the
upload_dirfilter may solve this by letting you override the calculated paths if used like this:add_filter('upload_dir', function($uploads) { $uploads['basedir'] = '/foo/bar/uploads'; $uploads['baseurl'] = 'https://example.com/uploads'; $uploads['path'] = $uploads['basedir'] . $uploads['subdir']; $uploads['url'] = $uploads['baseurl'] . $uploads['subdir']; return $uploads; });Can you confirm if this doesn't work for your use case?
Removing
needs-testingpending clarification.