wppaste
WordPress

wp_insert_site( array $data ): int|WP_Error

Since
5.1.0
Source
wp-includes/ms-site.php:44
Inserts a new site into the database.

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 string

    Site domain.
  • $pathstringdefault: '/'

    Site path.
  • $network_idintdefault: is the current network ID

    The site's network ID.
  • $registeredstringdefault: is the current time

    When the site was registered, in SQL datetime format.
  • $last_updatedstringdefault: is the value of $registered

    When the site was last updated, in SQL datetime format.
  • $publicintdefault: 1

    Whether the site is public.
  • $archivedintdefault: 0

    Whether the site is archived.
  • $matureintdefault: 0

    Whether the site is mature.
  • $spamintdefault: 0

    Whether the site is spam.
  • $deletedintdefault: 0

    Whether the site is deleted.
  • $lang_idintdefault: 0

    The site's language ID. Currently unused.
  • $user_idint

    User ID for the site administrator. Passed to the wp_initialize_site hook.
  • $titlestring

    Site title. Default is 'Site %d' where %d is the site ID. Passed to the wp_initialize_site hook.
  • $optionsarraydefault: empty array. Passed to the wp_initialize_site hook

    Custom option $key => $value pairs to use.
  • $metaarraydefault: empty array. Passed to the wp_initialize_site hook

    Custom 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

Reaches the database via ->insert().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
31–117

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 139.

Plugin surface
3 hooks

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.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action()called directly
  • optionnetwork optionget_network_option()called directly
  • sqldatabase query->insert()called directly
  • cacheobject cachewp_cache_delete()one call below wp_insert_site()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
is_wp_error()31current_time(), get_current_network_id(), wp_prepare_site_data(), is_wp_error()
!is_wp_error()49current_time(), get_current_network_id(), wp_prepare_site_data(), is_wp_error(), ->insert(), __(), domain()
!is_wp_error()56current_time(), get_current_network_id(), wp_prepare_site_data(), is_wp_error(), ->insert(), clean_blog_cache(), get_site(), __()
!is_wp_error() && !has_action()71–72current_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() && $meta106–109current_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() && !$meta114–117current_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

PHPCompiledExecutedBranchesNotes
8.6-dev13931–1178
8.513931–1178
8.413931–1178
8.313931–1178
8.213931–1178
8.113931–11782 fewer instructions than PHP 7.4
7.414131–1198

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:

  1. do_action( wp_insert_site )actionline 89 (+45 into the body)

    Fires once a site has been inserted into the database.

  2. do_action( wp_initialize_site )actionline 105 (+61 into the body)

    Fires when a site's initialization routine should be executed.

  3. do_action( wpmu_new_blog )action_deprecatedline 137 (+93 into the body)

    Fires immediately after a new site is created.

Uses · 12

Used by · 2

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.