[wp-trac] [WordPress Trac] #40279: switch_to_blog() and restore_current_blog() gives a fatal error in a multisite

WordPress Trac noreply at wordpress.org
Mon Mar 27 18:40:36 UTC 2017


#40279: switch_to_blog() and restore_current_blog() gives a fatal error in a
multisite
--------------------------------+-----------------------------
 Reporter:  KestutisIT          |      Owner:
     Type:  defect (bug)        |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Networks and Sites  |    Version:  4.7.3
 Severity:  normal              |   Keywords:
  Focuses:  multisite           |
--------------------------------+-----------------------------
 So I've built a multisite plugin, and my plugin uses namespaces, and that
 plugin does it's checks based on column.
 So it works well in multisite, but when site is not a multisite, plugin
 gives fatal error:
 {{{
 switch_to_blog($this->conf->getBlogId());
 }}}
 This also gives the fatal error (even despite that current blog is really
 = 1 when the multisite is off):
 {{{
 switch_to_blog(1);
 }}}
 And this also:
 {{{
 restore_current_blog();
 }}}

 The fatal error is like this:
 {{{
 Fatal error: Call to undefined function
 <MyPluginName>\Models\Booking\switch_to_blog() in C:\<..>\<MyWebsite>\wp-
 content\plugins\<MyPluginName>\Models\Booking\class.BookingsObserver.php
 on line 561.
 }}}

 And the error is really interesting, because it is trying to load that
 WordPress function, from my current namespace, while it don't do with any
 other WordPress function like that, so I guess there is a missing
 namespace definition in that class.

 A workaround is this code:
 {{{#!php
 <?php
 if(is_multisite())
 {
    switch_to_blog($this->conf->getBlogId());
 }
 }}}
 But it should not be mandatory.

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


More information about the wp-trac mailing list