Make WordPress Core

Opened 7 years ago

Closed 2 years ago

#40072 closed defect (bug) (duplicate)

URL from wp_get_attachment_image_src on multisite is wrong

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


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 (5)

#1 @antonioeatgoat
6 years 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.


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 5 years ago by antonioeatgoat (previous) (diff)

#2 in reply to: ↑ description @ideag
5 years 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.

#3 @gchtr
3 years ago

I just ran into this issue as well and made the same observation as @ideag when I wanted to load an image src from another blog using switch_to_blog().

#4 @decadx
2 years ago

I got this issue too, happens when using switch_to_blog() and try to call wp_get_attachment_url.

#5 @desrosj
2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

This one seems like a duplicate of #25650. Let's center all discussion in one place over there.

Note: See TracTickets for help on using tickets.