switch_to_blog()
switch_to_blog( int $new_blog_id, bool $deprecated = nu…
switch_to_blog( int $new_blog_id, bool $deprecated = null )
切换当前博客。
Switch the current blog.
切换当前博客。
Switch the current blog.
目录锚点:#说明#参数#源码#笔记
说明(Description)
如果您需要从其他博客中获取帖子或其他信息,此函数非常有用。您可以在以后使用restore_current_blog()返回。不能交换的东西:插件。请参见14941另请参阅restore_current_blog()
参数(Parameters)
| 参数 | 类型 | 说明 |
|---|---|---|
| $new_blog_id | (int) | 要切换到的日志的ID。默认值:当前博客。 |
| $deprecated | (bool) | 未使用。 |
源码(Source)
/**
* 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;
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| MU (3.0.0) | wp-includes/ms-blogs.php | 16 | 7 |
笔记(Notes)
一个开关
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!