On this page
restore_current_blog(): bool
Restore the current blog, after calling switch_to_blog() .
Description
See also
Return
bool True on success, false if we're already on the current blog.
More Information
restore_current_blog() should be called after every switch_to_blog(). If not, a global variable which monitors the switching, $GLOBALS['_wp_switched_stack'], will not be empty even if you use switch_to_blog() to return to the original blog. If $GLOBALS['_wp_switched_stack'] is not empty, WP will think it is in a switched state and can potentially return the wrong URL for the site via wp_upload_dir(). See http://wordpress.stackexchange.com/a/123516/27757
When calling switch_to_blog() repeatedly, either call restore_current_blog() each time, or save the original blog ID until the end and call switch_to_blog() with that and do:
$GLOBALS['_wp_switched_stack'] = array();
$GLOBALS['switched'] = false;
The former is probably preferable, as it is not a hack.
Source
File: wp-includes/ms-blogs.php. View all references
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',
'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, 'restore' );
// If we still have items in the switched stack, consider ourselves still 'switched'.
$GLOBALS['switched'] = ! empty( $GLOBALS['_wp_switched_stack'] );
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_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/restore_current_blog