get_site( WP_Site|int|null $site = null ): WP_Site|null
- Since
- 4.6.0
- Source
wp-includes/ms-site.php:310
Description
Site data will be cached and returned after being passed through a filter.
If the provided site is empty, the current site global will be used.
Compatibility
- WordPress
- since 4.6.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.
Parameters
$siteWP_Site|int|nulloptional- Site to retrieve. Default is the current site.Default:
null
Return value
WP_Site|null- The site object or null if not found.
Performance profile
How much work a call to get_site() 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
- 9–22
- Plugin surface
- 1 hook
- Called by
- 21
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, depending on the branch taken. The body compiles to 29.
Third-party callbacks on 'get_site' run inside this call, and their cost is not bounded by anything here.
21 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_site() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($site) | 9–14 | none |
empty($site) | 12–17 | get_current_blog_id() |
!empty($site) && !($site instanceof) && !is_object($site) | 13 | ::WP_Site() |
!empty($site) | 14–19 | apply_filters() |
empty($site) && !($site instanceof) && !is_object($site) | 16 | get_current_blog_id(), ::WP_Site() |
empty($site) | 17–22 | get_current_blog_id(), apply_filters() |
!empty($site) && !($site instanceof) && !is_object($site) | 18 | ::WP_Site(), apply_filters() |
empty($site) && !($site instanceof) && !is_object($site) | 21 | get_current_blog_id(), ::WP_Site(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 29 instructions, 9–22 executed per call, 4 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.
Hooks and filters fired · 1
One hook fires while get_site() runs, in this order:
Uses · 4
- get_current_blog_id()Retrieves the current site ID.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- WP_Site::__construct()Creates a new WP_Site object.
- WP_Site::get_instance()Retrieves a site from the database by its ID.
Used by · 21
- WP_Network::get_main_site_id()Returns the main site ID for the network.
- WP_REST_Users_Controller::create_item()Creates a single user.
- WP_Site_Query::get_sites()Retrieves a list of sites matching the query vars.
- add_user_to_blog()Adds a user to a blog, along with specifying the user's role.
- clean_blog_cache()Clean the blog cache
- get_active_blog_for_user()Gets one of a user's active blogs.
- get_blog_status()Gets a blog details field.
- get_blogaddress_by_id()Gets a full site URL, given a site ID.
- get_dashboard_blog()Get the "dashboard blog", the blog where users without a blog edit their profile data.
- is_user_member_of_blog()Finds out whether a user is a member of a given blog.
- ms_site_check()Checks status of current blog.
- wp_delete_site()Deletes a site from the database.
Show all 21
- wp_get_sites()Return an array of sites for a network or networks.
- wp_initialize_site()Runs the initialization routine for a given site.
- wp_insert_site()Inserts a new site into the database.
- wp_lostpassword_url()Returns the URL that allows the user to reset the lost password.
- wp_uninitialize_site()Runs the uninitialization routine for a given site.
- wp_update_site()Updates a site in the database.
- wp_xmlrpc_server::_multisite_getUsersBlogs()Private function for retrieving a users blogs for multisite setups.
- wpmu_delete_blog()Deletes a site.
- wpmu_new_site_admin_notification()Notifies the Multisite network administrator that a new site was created.
Source code
function get_site( $site = null ) { if ( empty( $site ) ) { $site = get_current_blog_id(); } if ( $site instanceof WP_Site ) { $_site = $site; } elseif ( is_object( $site ) ) { $_site = new WP_Site( $site ); } else { $_site = WP_Site::get_instance( $site ); } if ( ! $_site ) { return null; } /** * Fires after a site is retrieved. * * @since 4.6.0 * * @param WP_Site $_site Site data. */ $_site = apply_filters( 'get_site', $_site ); return $_site;}Changelog
Introduced in 4.6.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 6.8.8 tag, from
src/wp-includes/ms-site.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.