WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 11 months ago

#25650 new defect (bug)

When switching between blogs, wp_upload_dir 'baseurl' and 'url' may be pointing to the current blog not the switched one

Reported by: igmoweb Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.6.1
Component: Upload Keywords: needs-patch
Focuses: multisite Cc:

Description

Only tested on subfolder multisite (I don't have a way to test on a subdomain install right now).

It seems very similar to ticket #23483 but I'm not sure if it should be treated the same:

Example with two blogs in a network:

  • www.mydomain.com/my-source-blog (ID:5)
  • www.mydomain.com/my-destination-blog (ID:15)

I discover this while trying to move a post with images from a blog to another. The post is copied and images inside the post are copied too.

Starting point: The current blog ID is 15 (my-destination-blog). I'm trying to get upload URLs from the current blog:

switch_to_blog( 5 );
$source_upload_dir = wp_upload_dir();
$source_upload_baseurl = $source_upload_dir['baseurl'];
restore_current_blog();

$destination_upload_dir = wp_upload_dir();
$destination_upload_baseurl = $destination_upload_dir ['baseurl'];

At this point, $source_upload_baseurl is:

http://www.mydomain.com/my-destination-blog/wp-content/uploads/sites/5

Where should be:

http://www.mydomain.com/my-source-blog/wp-content/uploads/sites/5

$destination_upload_baseurl is fine.

It seems that if get_option( 'upload_url_path' ) returns false, wp_upload_dir() makes use of WP_CONTENT_URL constant that is set to the current blog URL at the beggining of the execution (in default-constants.php):

define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content');

This value obviously does not change when switching a blog.

Attachments (2)

25650_approach_1.patch (2.3 KB) - added by igmoweb 11 months ago.
First approach for a solution
25650_approach_2.patch (1.9 KB) - added by igmoweb 11 months ago.

Download all attachments as: .zip

Change History (13)

#1 @Denis-de-Bernardy
3 years ago

  • Keywords dev-feedback added

@nacin: Do we want this fixed in 3.9?

#2 follow-up: @igmoweb
3 years ago

I'm not completely sure but in case it helps, I think a fix would be replace current 1578 line in wp-includes/functions.php:

$url = WP_CONTENT_URL . '/uploads';

For something like

$url = get_option('siteurl') . '/wp-content/uploads';

But the problem now would be that the user could not change the content url by himself using WP_CONTENT_URL constant. Or at least, new issues would come, I guess.

#3 in reply to: ↑ 2 @Denis-de-Bernardy
3 years ago

Replying to igmoweb:

But the problem now would be that the user could not change the content url by himself using WP_CONTENT_URL constant. Or at least, new issues would come, I guess.

Yeah, it would likely be a regression. :-)

Something along the lines of filtering the uploads url might work, though (see #23221).

#4 @pavelevap
2 years ago

I probably ran into the same issue with subdomain install, but I am not sure...

Main site is on mydomain, secondary site is on subdomain.mydomain. After switch_to_blog(2) and calling the_post_thumbnail() there is problem with image URL on main site:

http://mydomain/wp-content/uploads/sites/2/2014/10/picture.jpg (does not exist)

But it should be:

http://subdomain.mydomain/wp-content/uploads/sites/2/2014/10/picture.jpg

#5 @yo-l1982
2 years ago

#30209 was marked as a duplicate.

#6 @flynsarmy
2 years ago

Here's a workaround I'm using until this is fixed:

add_action('switch_blog', function($new_blog, $prev_blog_id) {
	update_option( 'upload_url_path',  get_option('siteurl') . '/wp-content/uploads');
}, 10, 2);

#7 @jeremyfelt
22 months ago

  • Focuses multisite added

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


11 months ago

#9 @chriscct7
11 months ago

  • Keywords needs-patch added; dev-feedback removed

Bug Scrub 1/8/2015 notes:

  • The suggestion was made to introduce a function for which the current content directory can be retrieved at any time, thus solving the switch_to_blog bug. The suggested title for this function, which would replace the use of the constant of the same name is wp_content_dir()

@igmoweb
11 months ago

First approach for a solution

#10 @igmoweb
11 months ago

Hi, I attached a patch for a first approach to the problem ( 25650_approach_2.patch, forget about the first one). This was what I had in mind for this. Obviously, this is not fully working but it fixes the main problem.

So the idea is to stop trusting WP_CONTENT_URL constant and add a global $wp_content_url (maybe a static one is better, not sure right now). The global is actually based on the constant so the user can still define it in some way. When a switch between blogs is made we can use this global to change the content URL. Right now, I'm just replacing the sites URLs, what can be bad but it's a start.

A dev feedback would be handy in here.

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


11 months ago

Note: See TracTickets for help on using tickets.