返回介绍

switch_to_blog()

发布于 2017-09-11 01:56:58 字数 7490 浏览 1093 评论 0 收藏 0

switch_to_blog( int $new_blog,  bool $deprecated = null )

Switch the current blog.


description

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
  • plugins. See

参数

$new_blog

(int) (Required) The id of the blog you want to switch to. Default: current blog

$deprecated

(bool) (Optional) Deprecated argument

Default value: null


返回值

(true) Always returns True.


源代码

File: wp-includes/ms-blogs.php

function switch_to_blog( $new_blog, $deprecated = null ) {
	global $wpdb, $wp_roles;

	$blog_id = get_current_blog_id();
	if ( empty( $new_blog ) ) {
		$new_blog = $blog_id;
	}

	$GLOBALS['_wp_switched_stack'][] = $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 == $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 = $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', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
			}
			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
		}
	}

	if ( did_action( 'init' ) ) {
		$wp_roles = new WP_Roles();
		$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;
}

更新日志

Versiondescription
MUIntroduced.

相关函数

Uses

  • wp-includes/class-wp-roles.php: WP_Roles::__construct()
  • wp-includes/cache.php: wp_cache_switch_to_blog()
  • wp-includes/cache.php: wp_cache_init()
  • wp-includes/cache.php: wp_cache_add_global_groups()
  • wp-includes/cache.php: wp_cache_add_non_persistent_groups()
  • wp-includes/pluggable.php: wp_get_current_user()
  • wp-includes/load.php: get_current_blog_id()
  • wp-includes/plugin.php: do_action()
  • wp-includes/plugin.php: did_action()
  • wp-includes/ms-blogs.php: switch_blog
  • wp-includes/wp-db.php: wpdb::set_blog_id()
  • wp-includes/wp-db.php: wpdb::get_blog_prefix()
  • Show 7 more uses Hide more uses

Used By

  • wp-includes/class-wp-site.php: WP_Site::get_details()
  • wp-includes/general-template.php: has_custom_logo()
  • wp-includes/general-template.php: get_custom_logo()
  • wp-includes/general-template.php: get_site_icon_url()
  • wp-admin/includes/class-wp-ms-sites-list-table.php: WP_MS_Sites_List_Table::column_blogname()
  • wp-signup.php: confirm_another_blog_signup()
  • wp-admin/includes/ms.php: wpmu_delete_user()
  • wp-admin/includes/ms.php: upload_space_setting()
  • wp-admin/includes/ms.php: wpmu_delete_blog()
  • wp-admin/includes/class-wp-users-list-table.php: WP_Users_List_Table::get_views()
  • wp-admin/includes/class-wp-importer.php: WP_Importer::set_blog()
  • wp-includes/capabilities.php: current_user_can_for_blog()
  • wp-includes/class-wp-theme.php: WP_Theme::get_allowed_on_site()
  • wp-includes/link-template.php: get_home_url()
  • wp-includes/link-template.php: get_site_url()
  • wp-includes/admin-bar.php: wp_admin_bar_my_sites_menu()
  • wp-includes/ms-functions.php: wpmu_create_blog()
  • wp-includes/ms-functions.php: newblog_notify_siteadmin()
  • wp-includes/ms-functions.php: get_blog_post()
  • wp-includes/ms-functions.php: add_user_to_blog()
  • wp-includes/ms-functions.php: remove_user_from_blog()
  • wp-includes/ms-deprecated.php: create_empty_blog()
  • wp-includes/ms-functions.php: get_blog_permalink()
  • wp-includes/ms-blogs.php: update_blog_details()
  • wp-includes/ms-blogs.php: get_blog_option()
  • wp-includes/ms-blogs.php: add_blog_option()
  • wp-includes/ms-blogs.php: delete_blog_option()
  • wp-includes/ms-blogs.php: update_blog_option()
  • wp-includes/ms-blogs.php: get_blog_details()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::wp_getUsersBlogs()
  • Show 25 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    One switch

    
    switch_to_blog( $blog_id );
    // Do something
    restore_current_blog();
    
  2. Multiple switches

    
    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().

    description

    Restores previous blog after a switch_to_blog call.

    Contrary to the function’s name, this does NOT restore the original blog but the previous blog. Calling `switch_to_blog()` twice in a row and then calling this function will result in being on the blog set by the first `switch_to_blog()` call.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文