restore_current_blog()WP 3.0.0

Returns global variables to their previous state after switching to another site using switch_to_blog().

The function checks before execution whether a switch occurred; if not, it does nothing. This means that the function can be called even if no switch has taken place, it will not impact performance.

Important

The function must be called after each use of switch_to_blog(). If this is not done, the global variable: $GLOBALS['_wp_switched_stack'], which tracks the switch, will remain filled. WP will think you are in a switched state and may return the wrong URL for the site when using - wp_upload_dir(). This will happen even if switching to the current site - call switch_to_blog( ID ), where ID is the ID of the current blog.

When calling switch_to_blog() multiple times, call restore_current_blog() each time. Or save the blog ID and call switch_to_blog() with that ID at the end and clear the global variables:

$GLOBALS['_wp_switched_stack'] = array();
$GLOBALS['switched'] = false;
1 time — 0.001188 sec (very slow) | 50000 times — 0.059483 sec (speed of light)
Hooks from the function

Returns

true|false. true - on successful return and false - if already on the current site.

Usage

restore_current_blog();

Examples

0

#1 Returning to the current site of multisite (blog)

The example shows how to return to the current blog after switching to another blog and doing everything there:

// switch to blog 5
switch_to_blog( 5 );

// Output the data of the blog we switched to
// Getting posts from blog 5
$myposts = get_posts();
foreach( $myposts as $post ){
	echo esc_html( $post->post_title ) .'<br>';
}
wp_reset_postdata();

// return to the current blog
restore_current_blog();

Notes

  • See: switch_to_blog()
  • Global. wpdb. $wpdb WordPress database abstraction object.
  • Global. Array. $_wp_switched_stack
  • Global. Int. $blog_id
  • Global. true|false. $switched
  • Global. String. $table_prefix The database table prefix.
  • Global. WP_Object_Cache. $wp_object_cache

Changelog

Since 3.0.0 Introduced.

restore_current_blog() code WP 6.8.3

function restore_current_blog() {
	global $wpdb;

	if ( empty( $GLOBALS['_wp_switched_stack'] ) ) {
		return false;
	}

	$new_blog_id  = array_pop( $GLOBALS['_wp_switched_stack'] );
	$prev_blog_id = get_current_blog_id();

	if ( $new_blog_id === $prev_blog_id ) {
		/** This filter is documented in wp-includes/ms-blogs.php */
		do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );

		// If we still have items in the switched stack, consider ourselves still 'switched'.
		$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );

		return true;
	}

	$wpdb->set_blog_id( $new_blog_id );
	$GLOBALS['blog_id']      = $new_blog_id;
	$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();

	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
		wp_cache_switch_to_blog( $new_blog_id );
	} else {
		global $wp_object_cache;

		if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) {
			$global_groups = $wp_object_cache->global_groups;
		} else {
			$global_groups = false;
		}

		wp_cache_init();

		if ( function_exists( 'wp_cache_add_global_groups' ) ) {
			if ( is_array( $global_groups ) ) {
				wp_cache_add_global_groups( $global_groups );
			} else {
				wp_cache_add_global_groups(
					array(
						'blog-details',
						'blog-id-cache',
						'blog-lookup',
						'blog_meta',
						'global-posts',
						'image_editor',
						'networks',
						'network-queries',
						'sites',
						'site-details',
						'site-options',
						'site-queries',
						'site-transient',
						'theme_files',
						'rss',
						'users',
						'user-queries',
						'user_meta',
						'useremail',
						'userlogins',
						'userslugs',
					)
				);
			}

			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
		}
	}

	/** This filter is documented in wp-includes/ms-blogs.php */
	do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'restore' );

	// If we still have items in the switched stack, consider ourselves still 'switched'.
	$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );

	return true;
}