Opened 8 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 | Owned by: | |
---|---|---|---|
Milestone: | 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 (5)
#2
in reply to:
↑ description
@
6 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
@
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()
.
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) withswitch_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
.