[wp-trac] [WordPress Trac] #25293: Switch_to_blog not switching the siteid

WordPress Trac noreply at wordpress.org
Wed Aug 17 11:33:19 UTC 2016


#25293: Switch_to_blog not switching the siteid
--------------------------------+-----------------------------
 Reporter:  Rahe                |       Owner:
     Type:  defect (bug)        |      Status:  new
 Priority:  normal              |   Milestone:  Future Release
Component:  Networks and Sites  |     Version:  3.0
 Severity:  minor               |  Resolution:
 Keywords:                      |     Focuses:  multisite
--------------------------------+-----------------------------

Comment (by flixos90):

 For the above concept, it could look something like this:

 {{{
 function switch_to_site( $site, $switch_network = false ) {
         if ( $switch_network && ! is_object( $site ) ) {
                 $site = get_site( $site );
                 $site_id = $site->blog_id;
         } elseif ( is_object( $site ) ) {
                 $site_id = $site->blog_id;
                 $site = $site_id;
         } else {
                 $site_id = $site;
         }

         $GLOBALS['_wp_switched_sites_stack'][] = $site;

         // do rest of the regular logic with $site_id

         if ( $switch_network ) {
                 switch_to_network( $site->site_id );
         }
 }

 function restore_current_site() {
         if ( empty( $GLOBALS['_wp_switched_sites_stack'] ) ) {
                 return false;
         }

         $restore_network = false;

         $site = array_pop( $GLOBALS['_wp_switched_sites_stack'] );
         if ( is_object( $site ) ) {
                 $site_id = $site->blog_id;
                 $restore_network = true;
         } else {
                 $site_id = $site;
         }

         // do rest of the regular logic with $site_id

         if ( $restore_network ) {
                 restore_current_network();
         }
 }

 function switch_to_network( $network, $switch_site = false ) {
         if ( $switch_site && ! is_object( $network ) ) {
                 $network = get_network( $network );
                 $network_id = $network->id;
         } elseif ( is_object( $network ) ) {
                 $network_id = $network->id;
                 $network = $network_id;
         } else {
                 $network_id = $network;
         }

         $GLOBALS['_wp_switched_networks_stack'][] = $network;

         // do rest of the regular logic with $network_id

         if ( $switch_site ) {
                 switch_to_site( $network->blog_id );
         }
 }

 function restore_current_network() {
         if ( empty( $GLOBALS['_wp_switched_networks_stack'] ) ) {
                 return false;
         }

         $restore_site = false;

         $network = array_pop( $GLOBALS['_wp_switched_networks_stack'] );
         if ( is_object( $network ) ) {
                 $network_id = $network->id;
                 $restore_site = true;
         } else {
                 $network_id = $network;
         }

         // do rest of the regular logic with $network_id

         if ( $restore_site ) {
                 restore_current_site();
         }
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/25293#comment:9>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list