WordPress.org

Make WordPress Core

Opened 12 months ago

Last modified 11 months ago

#45725 new enhancement

Unable to use the UPLOADS constant with WordPress in a different directory

Reported by: Fleuv Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Upload Keywords: has-patch needs-testing needs-docs 2nd-opinion
Focuses: Cc:
PR Number:

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.

Attachments (1)

enhance_uploads_directory_path.diff (810 bytes) - added by Fleuv 12 months ago.

Download all attachments as: .zip

Change History (3)

This ticket was mentioned in Slack in #core by fleuv. View the logs.


11 months ago

#2 @pento
11 months ago

  • Version trunk deleted
Note: See TracTickets for help on using tickets.