[wp-trac] [WordPress Trac] #49335: On Multisite, wp_get_attachment_image_url returns URL with current site path even though switch_to_blog() is on another

WordPress Trac noreply at wordpress.org
Fri Jan 31 21:39:44 UTC 2020


#49335: On Multisite, wp_get_attachment_image_url returns URL with current site
path even though switch_to_blog() is on another
--------------------------------+-----------------------------
 Reporter:  aubreypwd           |      Owner:  (none)
     Type:  defect (bug)        |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Networks and Sites  |    Version:  5.3.2
 Severity:  normal              |   Keywords:
  Focuses:  multisite           |
--------------------------------+-----------------------------
 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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/49335>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list