Make WordPress Core

Opened 12 years ago

Last modified 7 years ago

#23483 new defect (bug)

Incorrect image URL for subsites when using UPLOADS constant on multisite subdirectory installation

Reported by: creativeinfusion's profile creativeinfusion Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.5.1
Component: Upload Keywords: needs-patch
Focuses: multisite Cc:

Description (last modified by SergeyBiryukov)

If the UPLOADS constant is used on a Wordpress Multisite installed to subdirectory, using subdirectory mode, then image URLs for subsites are incorrect.

Example:

  • WP MS installed to www.domain.com/wordpress, subdirectory not subdomain
  • UPLOADS set to 'assets'

Main site uploads images to /wordpress/assets/...
Main site image URL is www.domain.com/wordpress/assets/...

  1. Create subsite called 'subsite';
  2. Subsite uploads images to /wordpress/assets/sites/2/...
  3. Subsite image URL is www.domain.com/subsite/assets/sites/2/... when it should be www.domain.com/assets/sites/2/...

This is because wp_upload_dir() uses get_option('siteurl') to derive the URL. It is probably right for subdomain multisite but wrong in this use case.

Change History (12)

#1 @creativeinfusion
12 years ago

Tentative fix - check if multisite and not using subdomain install and use network_site_url instead

	if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) {
		$dir = ABSPATH . UPLOADS;
		if ( is_multsite() && is_subdomain_install() ) {
			$url = trailingslashit( $siteurl ) . UPLOADS;
		} else {
			$url = trailingslashit( network_site_url() ) . UPLOADS;
		}
	}

#2 @sabreuse
12 years ago

  • Component changed from General to Multisite

#3 @SergeyBiryukov
12 years ago

  • Description modified (diff)

#4 @creativeinfusion
12 years ago

Typo correction to point 3 (can't edit it myself)

Should read

  1. Subsite image URL is www.domain.com/subsite/assets/sites/2/... when it should be www.domain.com/assets/sites/2/...

#5 @creativeinfusion
12 years ago

Apologies, the tentative fix is too specific to the UPLOADS use case.

The site url is also used to deduce the url:

  • if upload_path option is specified and upload_url_path is not (this would also need a fix).
  • for old ms_files rewriting (probably does not need a fix).

#6 @creativeinfusion
12 years ago

Sorry about all the updates, but it's just dawned on me that the URL is set how it is by design, because in the absence of UPLOADS being used the subsite uploads url is just rewritten by
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*)

That rewrite doesn't handle the situation in this ticket when the UPLOADS folder is outside wp-content, making it similar to the reason behind the warning Subdirectory networks may not be fully compatible with custom wp-content directories (if I understand that correctly).

#7 @SergeyBiryukov
12 years ago

  • Description modified (diff)

#8 @creativeinfusion
12 years ago

This boils down to the URL being correct but the rewrite not supporting this situation.

As UPLOADS is always relative to ABSPATH, the fix for this would be to detect when uploads is not inside wp-content.

Entirely reasonable for this to solved with a manual rewrite edit, but maybe we should have an additional warning?

#9 @jeremyfelt
11 years ago

  • Component changed from Multisite to Upload
  • Focuses multisite added

#10 @Denis-de-Bernardy
11 years ago

A temporary fix in the meanwhile is here (use that instead of defining UPLOADS):

https://core.trac.wordpress.org/ticket/23221#comment:25

#11 @chriscct7
9 years ago

  • Keywords needs-patch added

#12 @natejacobson
7 years ago

This issue surfaced four years ago, but I'm experiencing @creativeinfusion's issue now in 2017. Following all the references above, it's unclear to me what the solution was.

The site subdirectory in multisite is present in the attachment url when a custom UPLOADS directory is specified. The upload succeeds, but the attachment url is incorrect and hence attachments fail to load.

Is a RewriteRule the only hope?

Nate

---

Update
After trying a host of other functions and filters to remedy the problem, what worked for me was simply this in functions.php:

update_option('upload_url_path', $_SERVER['HTTP_HOST'].'/media');
Version 3, edited 7 years ago by natejacobson (previous) (next) (diff)
Note: See TracTickets for help on using tickets.