switch_to_blog()切换到指定的博客(多站点)

目录

描述

Switch the current blog to a different blog. switch_to_blog() is useful if you need to pull posts or other information
from other blogs.

You can switch back afterwards using restore_current_blog(). Note that this function reverses only the last blog switching action, typically the most recent call to switch_to_blog(). See the example below on how to proceed when multiple switch_to_blog()s are used.

Things that aren't switched:

switch_to_blog() 描述

参数

$new_blog

(integer) (必填) The id of the blog you want to switch to.

默认值: current blog

$deprecated

(boolean) (可选) Deprecated argument

默认值: null

switch_to_blog() 参数

返回值

(bool

true is returned even when no blog exists with a Blog ID of $new_blog, or when "switching" to the current blog.

switch_to_blog() 返回值

示例

One switch

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

switch_to_blog( $blog_id );
// Do something
restore_current_blog();

Multiple switches

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

foreach( $blog_ids as $blog_id ){
    switch_to_blog( $blog_id );
    //Do stuff
    restore_current_blog();
}

If you do not call restore_current_blog() after every switch_to_blog(), WordPress can get into a state that can potentially build the wrong urls for the site.
See restore_current_blog() vs switch_to_blog()

switch_to_blog() 示例

历史

  • 添加于 版本: MU

switch_to_blog() 历史

源文件

switch_to_blog() 函数的代码位于 wp-includes/ms-blogs.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Switch the current blog.
 *
 * This function is useful if you need to pull posts, or other information,
 * from other blogs. You can switch back afterwards using restore_current_blog().
 *
 * Things that aren't switched:
 *  - autoloaded options. See #14992
 *  - plugins. See #14941
 *
 * @see restore_current_blog()
 * @since MU
 *
 * @global wpdb            $wpdb
 * @global int             $blog_id
 * @global array           $_wp_switched_stack
 * @global bool            $switched
 * @global string          $table_prefix
 * @global WP_Object_Cache $wp_object_cache
 *
 * @param int  $new_blog   The id of the blog you want to switch to. Default: current blog
 * @param bool $deprecated Deprecated argument
 * @return true Always returns True.
 */
function switch_to_blog( $new_blog, $deprecated = null ) {
	global $wpdb;

	if ( empty( $new_blog ) )
		$new_blog = $GLOBALS['blog_id'];

	$GLOBALS['_wp_switched_stack'][] = $GLOBALS['blog_id'];

	/*
	 * If we're switching to the same blog id that we're on,
	 * set the right vars, do the associated actions, but skip
	 * the extra unnecessary work
	 */
	if ( $new_blog == $GLOBALS['blog_id'] ) {
		/**
		 * Fires when the blog is switched.
		 *
		 * @since MU
		 *
		 * @param int $new_blog New blog ID.
		 * @param int $new_blog Blog ID.
		 */
		do_action( 'switch_blog', $new_blog, $new_blog );
		$GLOBALS['switched'] = true;
		return true;
	}

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

	if ( function_exists( 'wp_cache_switch_to_blog' ) ) {
		wp_cache_switch_to_blog( $new_blog );
	} 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( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache' ) );
			}
			wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
		}
	}

	if ( did_action( 'init' ) ) {
		wp_roles()->reinit();
		$current_user = wp_get_current_user();
		$current_user->for_blog( $new_blog );
	}

	/** This filter is documented in wp-includes/ms-blogs.php */
	do_action( 'switch_blog', $new_blog, $prev_blog_id );
	$GLOBALS['switched'] = true;

	return true;
}

switch_to_blog() 源文件