get_current_blog_id(): int
- Since
- 3.1.0
- Source
wp-includes/load.php:1481
Returns the ID of the site currently being served by reading the global $blog_id variable and passing it through absint(). On a single-site install this is normally 1, and on multisite it changes only after switch_to_blog() or restore_current_blog() runs. Pair it with get_current_network_id() when you need the network rather than the site, since the two are easy to confuse in multisite code.
Compatibility
- WordPress
- since 3.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Return value
int- Site ID.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Print the current site ID
A minimal call that shows what the function returns on the running site.
$site_id = get_current_blog_id();
echo esc_html( 'Current site ID: ' . $site_id );On a non-multisite install this will print 1.
Build a site-aware transient key for a cached post count
Use the site ID as part of a cache key so a caching function stays correct if the same code later runs in a multisite network.
$cache_key = 'publish_count_site_' . get_current_blog_id();
$counts = wp_count_posts();
set_transient( $cache_key, $counts->publish, HOUR_IN_SECONDS );
$cached_count = get_transient( $cache_key );
printf(
'Cached publish count for site %d: %d',
get_current_blog_id(),
(int) $cached_count
);Common problems and fixes · 4
- Why does get_current_blog_id() always return 1 on my site?
- Why did I get 0 when I called this very early in a plugin?
- How is this different from get_current_network_id()?
- Does the return value change when I call switch_to_blog()?
Why does get_current_blog_id() always return 1 on my site?
Why did I get 0 when I called this very early in a plugin?
How is this different from get_current_network_id()?
Does the return value change when I call switch_to_blog()?
Alternatives and related functions
get_current_network_id- When you need the ID of the multisite network rather than the individual site.
switch_to_blog- When you need to temporarily change which site's ID get_current_blog_id() reports and run code in that site's context.
get_blog_details- When you need more information about a site than just its numeric ID, such as its domain or path.
is_multisite- When your code needs to branch depending on whether the install is multisite at all before trusting a site ID.
Performance profile
How much work a call to get_current_blog_id() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 5
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 5.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_current_blog_id() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5 | absint() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 5 instructions, 5 executed per call, 0 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 1
- absint()Converts a value to non-negative integer.
Used by · 50
- WP_Admin_Bar::initialize()Initializes the admin bar.
- WP_Customize_Nav_Menu_Item_Setting::preview()Handle previewing the setting.
- WP_Customize_Nav_Menu_Item_Setting::value()Get the instance data for a given nav_menu_item setting.
- WP_Customize_Nav_Menu_Setting::filter_nav_menu_options()Filters the nav_menu_options option to include this menu's auto_add preference.
- WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menu_object()Filters the wp_get_nav_menu_object() result to supply the previewed menu object.
- WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()Filters the wp_get_nav_menus() result to ensure the inserted menu object is included, and the deleted one is removed.
- WP_Customize_Nav_Menu_Setting::preview()Handle previewing the setting.
- WP_Customize_Nav_Menu_Setting::value()Get the instance data for a given widget setting.
- WP_Customize_Setting::is_current_blog_previewed()Return true if the current site is not the same as the previewed site.
- WP_Customize_Setting::preview()Add filters to supply the setting's value when accessed.
- WP_Debug_Data::get_wp_core()Gets the WordPress core section of the debug data.
- WP_Object_Cache::__construct()Sets up object properties.
Show all 50
- WP_Roles::for_site()Sets the site to operate on. Defaults to the current site.
- WP_Roles::get_roles_data()Gets the available roles data.
- WP_Theme::get_allowed_on_site()Returns array of stylesheet names of themes allowed on the site.
- WP_Themes_List_Table::no_items()
- WP_User::for_site()Sets the site to operate on. Defaults to the current site.
- WP_User::get_role_caps()Retrieves all of the capabilities of the user's roles, and merges them with individual user capabilities.
- WP_User_Query::fill_query_vars()Fills in missing query variables with default values.
- WP_User_Query::get_cache_last_changed()Retrieves the last changed cache timestamp for users and optionally posts.
- _access_denied_splash()Displays an access denied message when a user tries to view a site's dashboard they do not have access to.
- _wp_upload_dir()A non-filtered, non-cached version of wp_upload_dir() that doesn't check the path.
- add_blog_option()Adds a new option for a given blog ID.
- add_existing_user_to_blog()Adds a user to a blog based on details from maybe_add_existing_user_to_blog().
- clean_site_details_cache()Cleans the site details cache for a site.
- count_users()Counts number of users who have each of the user roles.
- delete_blog_option()Removes an option by name for a given blog ID. Prevents removal of protected WordPress options.
- get_active_blog_for_user()Gets one of a user's active blogs.
- get_blog_details()Retrieves the details for a blog from the blogs table and blog options.
- get_blog_option()Retrieves option value for a given blog id based on name of option.
- get_blogs_of_user()Gets the sites a user belongs to.
- get_custom_logo()Returns a custom logo, linked to home unless the theme supports removing the link on the home page.
- get_dashboard_url()Retrieves the URL to the user's dashboard.
- get_main_site_id()Gets the main site ID.
- get_oembed_response_data_for_url()Retrieves the oEmbed response data for a given URL.
- get_site()Retrieves site data given a site ID or site object.
- get_site_icon_url()Returns the Site Icon URL.
- get_users_of_blog()Get users for the site.
- has_custom_logo()Determines whether the site has a custom logo.
- is_main_site()Determines whether a site is the main site of the current network.
- is_user_member_of_blog()Finds out whether a user is a member of a given blog.
- is_user_option_local()Check whether a usermeta key has to do with the current blog.
- ms_upload_constants()Defines Multisite upload constants.
- refresh_blog_details()Clears the blog details cache.
- restore_current_blog()Restores the current blog, after calling switch_to_blog().
- switch_to_blog()Switches the current blog.
- update_blog_option()Updates an option for a particular blog.
- update_blog_public()Updates this blog's 'public' setting in the global blogs table.
- upgrade_280()Execute changes made in WordPress 2.8.
- wp_admin_bar_site_menu()Adds the "Site Name" menu.
Source code
function get_current_blog_id() { global $blog_id; return absint( $blog_id );}Changelog
Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/load.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.