wp_insert_site( array $data ): int|WP_Error
- Since
- 5.1.0
- Source
wp-includes/ms-site.php:44
Compatibility
- WordPress
- since 5.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.
Parameters
$dataarray- Data for the new site that should be inserted.
$domainstringdefault: empty stringSite domain.$pathstringdefault: '/'Site path.$network_idintdefault: is the current network IDThe site's network ID.$registeredstringdefault: is the current timeWhen the site was registered, in SQL datetime format.$last_updatedstringdefault: is the value of $registeredWhen the site was last updated, in SQL datetime format.$publicintdefault: 1Whether the site is public.$archivedintdefault: 0Whether the site is archived.$matureintdefault: 0Whether the site is mature.$spamintdefault: 0Whether the site is spam.$deletedintdefault: 0Whether the site is deleted.$lang_idintdefault: 0The site's language ID. Currently unused.$user_idintUser ID for the site administrator. Passed to thewp_initialize_sitehook.$titlestringSite title. Default is 'Site %d' where %d is the site ID. Passed to thewp_initialize_sitehook.$optionsarraydefault: empty array. Passed to the wp_initialize_site hookCustom option $key => $value pairs to use.$metaarraydefault: empty array. Passed to the wp_initialize_site hookCustom site metadata $key => $value pairs to use.
Return value
int|WP_Error- The new site's ID on success, or error object on failure.
Performance profile
How much work a call to wp_insert_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
- Heavy
- Scaling
- Constant
- Instructions
- 31–117
- Plugin surface
- 3 hooks
- Called by
- 2
Reaches the database via ->insert().
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 139.
Third-party callbacks on 'wp_insert_site', 'wp_initialize_site', 'wpmu_new_blog' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - optionnetwork option
get_network_option()called directly - sqldatabase query
->insert()called directly - cacheobject cache
wp_cache_delete()one call below wp_insert_site() - serializeserialisation
maybe_unserialize()one call below wp_insert_site()
Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_insert_site() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
is_wp_error() | 31 | current_time(), get_current_network_id(), wp_prepare_site_data(), is_wp_error() |
!is_wp_error() | 49 | current_time(), get_current_network_id(), wp_prepare_site_data(), is_wp_error(), ->insert(), __(), domain() |
!is_wp_error() | 56 | current_time(), get_current_network_id(), wp_prepare_site_data(), is_wp_error(), ->insert(), clean_blog_cache(), get_site(), __() |
!is_wp_error() && !has_action() | 71–72 | current_time(), get_current_network_id(), wp_prepare_site_data(), is_wp_error(), ->insert(), clean_blog_cache(), get_site(), do_action(), array_diff_key(), do_action(), has_action() |
!is_wp_error() && has_action() && $meta | 106–109 | current_time(), get_current_network_id(), wp_prepare_site_data(), is_wp_error(), ->insert(), clean_blog_cache(), get_site(), do_action(), array_diff_key(), do_action(), has_action(), array_intersect_key(), array_merge() |
!is_wp_error() && has_action() && !$meta | 114–117 | current_time(), get_current_network_id(), wp_prepare_site_data(), is_wp_error(), ->insert(), clean_blog_cache(), get_site(), do_action(), array_diff_key(), do_action(), has_action(), get_network_option(), array_intersect_key(), array_merge() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 139 | 31–117 | 8 | |
| 8.5 | 139 | 31–117 | 8 | |
| 8.4 | 139 | 31–117 | 8 | |
| 8.3 | 139 | 31–117 | 8 | |
| 8.2 | 139 | 31–117 | 8 | |
| 8.1 | 139 | 31–117 | 8 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 141 | 31–119 | 8 |
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 · 3
3 hooks fire while wp_insert_site() runs, in this order:
- do_action( wp_insert_site )actionline 89 (+45 into the body)
Fires once a site has been inserted into the database.
- do_action( wp_initialize_site )actionline 105 (+61 into the body)
Fires when a site's initialization routine should be executed.
- do_action( wpmu_new_blog )action_deprecatedline 137 (+93 into the body)
Fires immediately after a new site is created.
Uses · 12
- current_time()Retrieves the current time based on specified type.
- get_current_network_id()Retrieves the current network ID.
- wp_prepare_site_data()Prepares site data for insertion or update in the database.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- clean_blog_cache()Clean the blog cache
- get_site()Retrieves site data given a site ID or site object.
- do_action()Calls the callback functions that have been added to an action hook.
- has_action()Checks if any action has been registered for a hook.
- get_network_option()Retrieves a network's option value based on the option name.
- do_action_deprecated()Fires functions attached to a deprecated action hook.
- WP_Error::__construct()Initializes the error.
Used by · 2
- insert_blog()Store basic site info in the blogs table.
- wpmu_create_blog()Creates a site.
Source code
function wp_insert_site( array $data ) { global $wpdb; $now = current_time( 'mysql', true ); $defaults = array( 'domain' => '', 'path' => '/', 'network_id' => get_current_network_id(), 'registered' => $now, 'last_updated' => $now, 'public' => 1, 'archived' => 0, 'mature' => 0, 'spam' => 0, 'deleted' => 0, 'lang_id' => 0, ); $prepared_data = wp_prepare_site_data( $data, $defaults ); if ( is_wp_error( $prepared_data ) ) { return $prepared_data; } if ( false === $wpdb->insert( $wpdb->blogs, $prepared_data ) ) { return new WP_Error( 'db_insert_error', __( 'Could not insert site into the database.' ), $wpdb->last_error ); } $site_id = (int) $wpdb->insert_id; clean_blog_cache( $site_id ); $new_site = get_site( $site_id ); if ( ! $new_site ) { return new WP_Error( 'get_site_error', __( 'Could not retrieve site data.' ) ); } /** * Fires once a site has been inserted into the database. * * @since 5.1.0 * * @param WP_Site $new_site New site object. */ do_action( 'wp_insert_site', $new_site ); // Extract the passed arguments that may be relevant for site initialization. $args = array_diff_key( $data, $defaults ); if ( isset( $args['site_id'] ) ) { unset( $args['site_id'] ); } /** * Fires when a site's initialization routine should be executed. * * @since 5.1.0 * * @param WP_Site $new_site New site object. * @param array $args Arguments for the initialization. */ do_action( 'wp_initialize_site', $new_site, $args ); // Only compute extra hook parameters if the deprecated hook is actually in use. if ( has_action( 'wpmu_new_blog' ) ) { $user_id = ! empty( $args['user_id'] ) ? $args['user_id'] : 0; $meta = ! empty( $args['options'] ) ? $args['options'] : array(); // WPLANG was passed with `$meta` to the `wpmu_new_blog` hook prior to 5.1.0. if ( ! array_key_exists( 'WPLANG', $meta ) ) { $meta['WPLANG'] = get_network_option( $new_site->network_id, 'WPLANG' ); } /* * Rebuild the data expected by the `wpmu_new_blog` hook prior to 5.1.0 using allowed keys. * The `$allowed_data_fields` matches the one used in `wpmu_create_blog()`. */ $allowed_data_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' ); $meta = array_merge( array_intersect_key( $data, array_flip( $allowed_data_fields ) ), $meta );Changelog
Introduced in 5.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 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.