wppaste
WordPress

wp_prepare_site_data( array $data, array $defaults, WP_Site|null $old_site = null ): array|WP_Error

Since
5.1.0
Source
wp-includes/ms-site.php:464
Prepares site data for insertion or update in 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
Associative array of site data passed to the respective function.
See wp_insert_site() for the possibly included data.
$defaultsarray
Site data defaults to parse $data against.
$old_siteWP_Site|nulloptional
Old site object if an update, or null if an insertion.
Default null.Default: null

Return value

array|WP_Error
Site data ready for a database transaction, or WP_Error in case a validation error occurred.

Performance profile

How much work a call to wp_prepare_site_data() 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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
31–43

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

Plugin surface
2 hooks

Third-party callbacks on 'wp_normalize_site_data', 'wp_validate_site_data' 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 callbacksapply_filters()called directly

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 wp_prepare_site_data() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always31–43apply_filters(), wp_parse_args(), array_intersect_key(), do_action()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 44 instructions, 31–43 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 · 2

2 hooks fire while wp_prepare_site_data() runs, in this order:

  1. apply_filters( wp_normalize_site_data )filterline 482 (+18 into the body)

    Filters passed site data in order to normalize it.

  2. do_action( wp_validate_site_data )actionline 502 (+38 into the body)

    Fires when data should be validated for a site prior to inserting or updating in the database.

Uses · 4

Used by · 2

Source code

function wp_prepare_site_data( $data, $defaults, $old_site = null ) { 	// Maintain backward-compatibility with `$site_id` as network ID.	if ( isset( $data['site_id'] ) ) {		if ( ! empty( $data['site_id'] ) && empty( $data['network_id'] ) ) {			$data['network_id'] = $data['site_id'];		}		unset( $data['site_id'] );	} 	/**	 * Filters passed site data in order to normalize it.	 *	 * @since 5.1.0	 *	 * @param array $data Associative array of site data passed to the respective function.	 *                    See {@see wp_insert_site()} for the possibly included data.	 */	$data = apply_filters( 'wp_normalize_site_data', $data ); 	$allowed_data_fields = array( 'domain', 'path', 'network_id', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );	$data                = array_intersect_key( wp_parse_args( $data, $defaults ), array_flip( $allowed_data_fields ) ); 	$errors = new WP_Error(); 	/**	 * Fires when data should be validated for a site prior to inserting or updating in the database.	 *	 * Plugins should amend the `$errors` object via its `WP_Error::add()` method.	 *	 * @since 5.1.0	 *	 * @param WP_Error     $errors   Error object to add validation errors to.	 * @param array        $data     Associative array of complete site data. See {@see wp_insert_site()}	 *                               for the included data.	 * @param WP_Site|null $old_site The old site object if the data belongs to a site being updated,	 *                               or null if it is a new site being inserted.	 */	do_action( 'wp_validate_site_data', $errors, $data, $old_site ); 	if ( ! empty( $errors->errors ) ) {		return $errors;	} 	// Prepare for database.	$data['site_id'] = $data['network_id'];	unset( $data['network_id'] ); 	return $data;}

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 7.1.0 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.