WordPress.org

Make WordPress Core

Opened 9 months ago

#49335 new defect (bug)

On Multisite, wp_get_attachment_image_url returns URL with current site path even though switch_to_blog() is on another

Reported by: aubreypwd Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3.2
Component: Networks and Sites Keywords:
Focuses: multisite Cc:

Description

Also see https://github.com/aubreypwd/WordPress/issues/6


So in my example, I have a sub-dir site with three sites:

1) The root site (ID: 1)
2) site-2 (ID: 2)
3) site-3 (ID: 3)

Then I load up site 2 wp-admin/ and let the following code run in an mu-plugin/:

add_action( 'plugins_loaded', function() {
	switch_to_blog( 3 );
		error_log( print_r( (object) [ __FILE__ => __LINE__,
			wp_get_attachment_image_url( 4, 'full' ),
			get_site( get_current_blog_id() ),
		], true ) );
	restore_current_blog();
} );

This switches to site 3 and gets an attachment URL using wp_get_attachment_image_url and outputs the WP_Site of site 3, but in the output:

(
    [/app/public/wp-content/mu-plugins/debug.php] => 9
    [0] => http://wordpress-subdir.test/site-2/wp-content/uploads/sites/3/2020/01/Database.png
    [1] => WP_Site Object
        (
            [blog_id] => 3
            [domain] => wordpress-subdir.test
            [path] => /site-3/
            [site_id] => 1
            [registered] => 2020-01-31 20:43:10
            [last_updated] => 2020-01-31 20:43:10
            [public] => 1
            [archived] => 0
            [mature] => 0
            [spam] => 0
            [deleted] => 0
            [lang_id] => 0
        )
)

Notice that the URL [0] => http://wordpress-subdir.test/site-2/wp-content/uploads/sites/3/2020/01/Database.png contains the site slug site-2 of site 2 (of the Dashboard I ran this from) even though we are actively on site 3 via switch_to_blog().

Oddly, on sub-domain I get:

(
    [/app/public/wp-content/mu-plugins/debug.php] => 9
    [0] => http://site2.wordpress-subdomain.test/wp-content/uploads/sites/3/2020/01/terminal-gnome.ong_.png
    [1] => WP_Site Object
        (
            [blog_id] => 3
            [domain] => site3.wordpress-subdomain.test
            [path] => /
            [site_id] => 1
            [registered] => 2020-01-31 21:28:57
            [last_updated] => 2020-01-31 21:28:57
            [public] => 1
            [archived] => 0
            [mature] => 0
            [spam] => 0
            [deleted] => 0
            [lang_id] => 0
        )

)

Which does NOT contain the site slug of the admin dashboard.

Change History (0)

Note: See TracTickets for help on using tickets.