WordPress.org

Make WordPress Core

Opened 22 months ago

Last modified 6 weeks ago

#25293 new defect (bug)

Switch_to_blog not switching the siteid

Reported by: Rahe Owned by:
Milestone: Future Release Priority: normal
Severity: minor Version: 3.0
Component: Networks and Sites Keywords:
Focuses: multisite Cc:

Description

When having multiple network on multisite making the following:

switch_to_blog(1);

$options = get_site_option( 'my_option' );

restore_current_blog();

The options retrieved are the options of the current siteid and not the siteid of the switched blog.
One of the options is to make something like this :

global $wpdb;
// Get the previous siteid
$previous_site_id = $wpdb->siteid;
$previous_blog_id = $wpdb->blogid;
		
// Go to site 1
switch_to_blog(1);
		
// Set the blog siteid to 1
$wpdb->set_blog_id( 1, 1 );

// Get the options
$options = get_site_option( 'my_option' );
		
restore_current_blog();
$wpdb->set_blog_id( $previous_blog_id , $previous_site_id );

Or

// Get the previous siteid
$site_id = $wpdb->siteid;
		
// Set the blog siteid to 1
$wpdb->set_blog_id( $wpdb->blogid, 1 );
	
// Get the options
$options = get_site_option( 'my_options' );
	
$wpdb->set_blog_id( $wpdb->blogid , $site_id );

The thing is that the switch_to_blog function does not specify the switched siteid on the method $wpdb->set_blog_id if the network is not the same as the current blog.

Change History (4)

comment:1 @jeremyfelt22 months ago

  • Version changed from 3.6.1 to 3.0

One of the great things about switch_to_blog() is that it requires no database operation to succeed. It is passed a $blog_id (which should be named $site_id), which it sets to the $_GLOBALS['blog_id'], it performs $wpdb->set_blog_id() to get the correct table prefixes setup for when queries are made, and it loads in the user's capabilities for the new $blog_id.

I don't think we want to introduce a database call to find the site's network ID from the $blog_id that was passed. It would be interesting to have switch_to_blog() accept another parameter for $network_id so that the caller could explicitly determine what site and network should be available. The deprecated parameter that is there now was $validate until [13125] it looks like.

More refreshing would be a new switch_to_site( $site_id, $network_id ) function that wraps switch_to_blog() and a switch_to_network( $network_id ) function to try and limit some of the confusion at the code level. Even then, it may be that passing $network_id is a decision left to the developer as the one who knows whether a network level lookup will be needed.

comment:2 @jeremyfelt18 months ago

  • Milestone changed from Awaiting Review to Future Release

comment:3 @jeremyfelt18 months ago

  • Component changed from Multisite to Networks and Sites
  • Focuses multisite added

comment:4 @slackbot6 weeks ago

This ticket was mentioned in Slack in #core-multisite by jeremyfelt. View the logs.

Note: See TracTickets for help on using tickets.