On this page
wp_prepare_site_data( array $data, array $defaults, WP_Site|null $old_site = null ): array|WP_Error
Prepares site data for insertion or update in the database.
Parameters
$dataarray Required-
Associative array of site data passed to the respective function.
See wp_insert_site() for the possibly included data.More Arguments from wp_insert_site( ... $data )
Data for the new site that should be inserted.
domainstringSite domain. Default empty string.pathstringSite path. Default'/'.network_idintThe site's network ID. Default is the current network ID.registeredstringWhen the site was registered, in SQL datetime format. Default is the current time.last_updatedstringWhen the site was last updated, in SQL datetime format. Default is the value of $registered.publicintWhether the site is public. Default 1.archivedintWhether the site is archived. Default 0.matureintWhether the site is mature. Default 0.spamintWhether the site is spam. Default 0.deletedintWhether the site is deleted. Default 0.lang_idintThe site's language ID. Currently unused. Default 0.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.optionsarrayCustom option $key => $value pairs to use. Default empty array. Passed to thewp_initialize_sitehook.metaarrayCustom site metadata $key => $value pairs to use. Default empty array.
Passed to thewp_initialize_sitehook.
$defaultsarray Required-
Site data defaults to parse $data against.
$old_siteWP_Site|null Optional-
Old site object if an update, or null if an insertion.
Default:
null
Return
array|WP_Error Site data ready for a database transaction, or WP_Error in case a validation error occurred.
Source
File: wp-includes/ms-site.php. View all references
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;
}
Hooks
- apply_filters( 'wp_normalize_site_data',
array $data ) -
Filters passed site data in order to normalize it.
- do_action( 'wp_validate_site_data',
WP_Error $errors ,array $data ,WP_Site|null $old_site ) -
Fires when data should be validated for a site prior to inserting or updating in the database.
Related
Uses
| Uses | Description |
|---|---|
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| wp_insert_site() wp-includes/ms-site.php | Inserts a new site into the database. |
| wp_update_site() wp-includes/ms-site.php | Updates a site in the database. |
Changelog
| Version | Description |
|---|---|
| 5.1.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_prepare_site_data