WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 3 weeks ago

#40072 new defect (bug)

URL from wp_get_attachment_image_src on multisite is wrong

Reported by: contremaitre Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version: 4.7.3
Component: General Keywords: needs-patch 2nd-opinion
Focuses: multisite Cc:

Description

wp_get_attachment_image_src returns the URL from the main site instead of the site currently visited, in case of multisite setup.
I tracked down the issue, up to :

  • image_downsize in wp-includes/media.php
  • wp_get_attachment_url in wp-includes/post.php
  • wp_get_upload_dir in functions.php

The wp_get_upload_dir URL and BASEURL in the returned array have the main site url instead of the multise one.

Change History (2)

#1 @antonioeatgoat
4 months ago

  • Keywords needs-patch 2nd-opinion added
  • Severity changed from normal to major

Hello @contremaitre, I faced it too yesterday. After some investigation I would add more details. It doesn't always return the url containing the domain of the main site, but with the domain of the site currently displaying.

Example:

I am on site1.multisite.test (id 2),
I switch to site2.multisite.test (id 3) with switch_to_blog(3),
The url returned is: http://site1.multisite.test/wp-content/uploads/sites/3.
The expected result would be instead http://site2.multisite.test/wp-content/uploads/sites/3.

Last edited 4 months ago by antonioeatgoat (previous) (diff)

#2 in reply to: ↑ description @ideag
3 weeks ago

  • Focuses multisite added

We also ran into this situation recently. We managed to narrow it down to the following situation. All of these have to be met:

  • A modern (post 3.5) multisite
  • default or empty upload_path option
  • switch to a different blog via switch_to_blog() function

The core of the problem is in _wp_upload_dir() function. Because upload_path is default or empty, it satisfies the clause on line 2139, so $url is defined the WP_CONTENT_URL constant (which holds the url of the original site, not the one that we switched to). And the multisite logic on lines 2156-2199 just appends site/x to $url, not overwriting it.

Note: See TracTickets for help on using tickets.