On this page
switch_to_blog( int $new_blog_id, bool $deprecated = null ): true
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:
- plugins. See #14941
See also
Parameters
$new_blog_idint Required-
The ID of the blog to switch to. Default: current blog.
$deprecatedbool Optional-
Not used.
Default:
null
Return
true Always returns true.
Source
File: wp-includes/ms-blogs.php. View all references
function switch_to_blog( $new_blog_id, $deprecated = null ) {
global $wpdb;
$prev_blog_id = get_current_blog_id();
if ( empty( $new_blog_id ) ) {
$new_blog_id = $prev_blog_id;
}
$GLOBALS['_wp_switched_stack'][] = $prev_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_id == $prev_blog_id ) {
/**
* Fires when the blog is switched.
*
* @since MU (3.0.0)
* @since 5.4.0 The `$context` parameter was added.
*
* @param int $new_blog_id New blog ID.
* @param int $prev_blog_id Previous blog ID.
* @param string $context Additional context. Accepts 'switch' when called from switch_to_blog()
* or 'restore' when called from restore_current_blog().
*/
do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
$GLOBALS['switched'] = true;
return true;
}
$wpdb->set_blog_id( $new_blog_id );
$GLOBALS['table_prefix'] = $wpdb->get_blog_prefix();
$GLOBALS['blog_id'] = $new_blog_id;
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',
'networks',
'sites',
'site-details',
'site-options',
'site-transient',
'rss',
'users',
'useremail',
'userlogins',
'usermeta',
'user_meta',
'userslugs',
)
);
}
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
}
}
/** This filter is documented in wp-includes/ms-blogs.php */
do_action( 'switch_blog', $new_blog_id, $prev_blog_id, 'switch' );
$GLOBALS['switched'] = true;
return true;
}
Hooks
- do_action( 'switch_blog',
int $new_blog_id ,int $prev_blog_id ,string $context ) -
Fires when the blog is switched.
Related
Uses
| Uses | Description |
|---|---|
| wp_cache_switch_to_blog() wp-includes/cache.php | Switches the internal blog ID. |
| wp_cache_init() wp-includes/cache.php | Sets up Object Cache Global and assigns it. |
| wp_cache_add_global_groups() wp-includes/cache.php | Adds a group or set of groups to the list of global groups. |
| wp_cache_add_non_persistent_groups() wp-includes/cache.php | Adds a group or set of groups to the list of non-persistent groups. |
| wpdb::set_blog_id() wp-includes/class-wpdb.php | Sets blog ID. |
| wpdb::get_blog_prefix() wp-includes/class-wpdb.php | Gets blog prefix. |
| get_current_blog_id() wp-includes/load.php | Retrieve the current site ID. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
Used By
| Used By | Description |
|---|---|
| wp_initialize_site() wp-includes/ms-site.php | Runs the initialization routine for a given site. |
| wp_uninitialize_site() wp-includes/ms-site.php | Runs the uninitialization routine for a given site. |
| wp_is_site_initialized() wp-includes/ms-site.php | Checks whether a site is initialized. |
| get_oembed_response_data_for_url() wp-includes/embed.php | Retrieves the oEmbed response data for a given URL. |
| WP_Site::get_details() wp-includes/class-wp-site.php | Retrieves the details for this site. |
| has_custom_logo() wp-includes/general-template.php | Determines whether the site has a custom logo. |
| get_custom_logo() wp-includes/general-template.php | Returns a custom logo, linked to home unless the theme supports removing the link on the home page. |
| wp_get_users_with_no_role() wp-includes/user.php | Gets the user IDs of all users with no role on this site. |
| get_site_icon_url() wp-includes/general-template.php | Returns the Site Icon URL. |
| WP_MS_Sites_List_Table::column_blogname() wp-admin/includes/class-wp-ms-sites-list-table.php | Handles the site name column output. |
| confirm_another_blog_signup() wp-signup.php | Shows a message confirming that the new site has been created. |
| wpmu_delete_user() wp-admin/includes/ms.php | Delete a user from the network and remove from all sites. |
| upload_space_setting() wp-admin/includes/ms.php | Displays the site upload space quota setting form on the Edit Site Settings screen. |
| wpmu_delete_blog() wp-admin/includes/ms.php | Delete a site. |
| WP_Users_List_Table::get_views() wp-admin/includes/class-wp-users-list-table.php | Return an associative array listing all the views that can be used with this table. |
| WP_Importer::set_blog() wp-admin/includes/class-wp-importer.php | |
| WP_User::get_role_caps() wp-includes/class-wp-user.php | Retrieves all of the capabilities of the user’s roles, and merges them with individual user capabilities. |
| current_user_can_for_blog() wp-includes/capabilities.php | Returns whether the current user has the specified capability for a given site. |
| WP_Theme::get_allowed_on_site() wp-includes/class-wp-theme.php | Returns array of stylesheet names of themes allowed on the site. |
| get_home_url() wp-includes/link-template.php | Retrieves the URL for a given site where the front end is accessible. |
| get_site_url() wp-includes/link-template.php | Retrieves the URL for a given site where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible. |
| wp_admin_bar_my_sites_menu() wp-includes/admin-bar.php | Adds the “My Sites/[Site Name]” menu and all submenus. |
| count_users() wp-includes/user.php | Counts number of users who have each of the user roles. |
| newblog_notify_siteadmin() wp-includes/ms-functions.php | Notifies the network admin that a new site has been activated. |
| create_empty_blog() wp-includes/ms-deprecated.php | Create an empty blog. |
| get_blog_post() wp-includes/ms-functions.php | Gets a blog post from any site on the network. |
| add_user_to_blog() wp-includes/ms-functions.php | Adds a user to a blog, along with specifying the user’s role. |
| remove_user_from_blog() wp-includes/ms-functions.php | Removes a user from a blog. |
| get_blog_permalink() wp-includes/ms-functions.php | Gets the permalink for a post on another blog. |
| get_blog_option() wp-includes/ms-blogs.php | Retrieve option value for a given blog id based on name of option. |
| add_blog_option() wp-includes/ms-blogs.php | Add a new option for a given blog ID. |
| delete_blog_option() wp-includes/ms-blogs.php | Removes option by name for a given blog ID. Prevents removal of protected WordPress options. |
| update_blog_option() wp-includes/ms-blogs.php | Update an option for a particular blog. |
| get_blog_details() wp-includes/ms-blogs.php | Retrieve the details for a blog from the blogs table and blog options. |
| wp_xmlrpc_server::wp_getUsersBlogs() wp-includes/class-wp-xmlrpc-server.php | Retrieve the blogs of the user. |
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/switch_to_blog