On this page
wp_insert_site( array $data ): int|WP_Error
Inserts a new site into the database.
Parameters
$dataarray Required-
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.
Return
int|WP_Error The new site's ID on success, or error object on failure.
Source
File: wp-includes/ms-site.php. View all references
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 );
/**
* Fires immediately after a new site is created.
*
* @since MU (3.0.0)
* @deprecated 5.1.0 Use {@see 'wp_initialize_site'} instead.
*
* @param int $site_id Site ID.
* @param int $user_id User ID.
* @param string $domain Site domain.
* @param string $path Site path.
* @param int $network_id Network ID. Only relevant on multi-network installations.
* @param array $meta Meta data. Used to set initial site options.
*/
do_action_deprecated(
'wpmu_new_blog',
array( $new_site->id, $user_id, $new_site->domain, $new_site->path, $new_site->network_id, $meta ),
'5.1.0',
'wp_initialize_site'
);
}
return (int) $new_site->id;
}
Hooks
- do_action_deprecated( 'wpmu_new_blog',
int $site_id ,int $user_id ,string $domain ,string $path ,int $network_id ,array $meta ) -
Fires immediately after a new site is created.
- do_action( 'wp_initialize_site',
WP_Site $new_site ,array $args ) -
Fires when a site’s initialization routine should be executed.
- do_action( 'wp_insert_site',
WP_Site $new_site ) -
Fires once a site has been inserted into the database.
Related
Uses
| Uses | Description |
|---|---|
| wp_prepare_site_data() wp-includes/ms-site.php | Prepares site data for insertion or update in the database. |
| get_current_network_id() wp-includes/load.php | Retrieves the current network ID. |
| get_site() wp-includes/ms-site.php | Retrieves site data given a site ID or site object. |
| do_action_deprecated() wp-includes/plugin.php | Fires functions attached to a deprecated action hook. |
| get_network_option() wp-includes/option.php | Retrieves a network’s option value based on the option name. |
| current_time() wp-includes/functions.php | Retrieves the current time based on specified type. |
| has_action() wp-includes/plugin.php | Checks if any action has been registered for a hook. |
| clean_blog_cache() wp-includes/ms-site.php | Clean the blog cache |
| wpdb::insert() wp-includes/class-wpdb.php | Inserts a row into the table. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| insert_blog() wp-includes/ms-deprecated.php | Store basic site info in the blogs table. |
| wpmu_create_blog() wp-includes/ms-functions.php | Creates a site. |
Changelog
| Version | Description |
|---|---|
| 5.1.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_insert_site