wp_initialize_site()
Prepares a new site in WordPress Multisite: creates database tables, fills in standard options, roles, metadata, and the site’s initial content.
The function is used to initialize a site that has already been added to the network but has not yet been prepared for operation. Usually it is not called directly, but via the site creation process from wp_insert_site().
During execution, the function switches to the required site through switch_to_blog(), creates tables, sets the basic options home, siteurl, blogname, blog_public, adds standard roles, creates initial posts/pages, and assigns the specified user as the site administrator.
The function is intended for Multisite. If the site has already been initialized, a repeated run will return an WP_Error to avoid overwriting the already created site structure.
Initialization arguments can be modified via the wp_initialize_site_args filter. It is convenient when you need to set your own options or metadata for all new sites in the network.
Hooks from the function
Returns
true|WP_Error.
true— the site was successfully initialized.WP_Error— an error occurred: the site ID was not provided, the site was not found, or the site has already been initialized.
Usage
wp_initialize_site( $site_id, $args );
- $site_id(int|WP_Site) (required)
- The site ID or an WP_Site{} object that needs to be initialized.
- $args(array)
- Initialization arguments that change the behavior.
Default:[] user_id(int)- The ID of the user who will become the administrator of the new site.
title(string)- The name of the site. If not specified, a string of the form
Site %dis used, where%dis the site ID. options(array)- An array of additional site options in the format
key => value. Passed to populate_options() and can override the default options. meta(array)- An array of site metadata in the format
key => value. Passed to populate_site_meta().
Examples
#1 Initializing a New Site
The example receives the ID of an already created site and runs the standard initialization for it.
$result = wp_initialize_site( 123, [
'user_id' => 1,
'title' => 'New site',
] );
if ( is_wp_error( $result ) ) {
wp_die( $result->get_error_message() );
} #2 Passing your options during initialization
The example sets the site name, the administrator email, and closes the site from indexing via the blog_public option.
$result = wp_initialize_site( 123, [ 'user_id' => 1, 'title' => 'Private site', 'options' => [ 'admin_email' => '[email protected]', 'blog_public' => 0, ], ] ); if ( is_wp_error( $result ) ) { error_log( $result->get_error_message() ); }
#3 Adding Your Site Metadata
The example adds metadata that will be saved for the new site during initialization.
$result = wp_initialize_site( 123, [
'user_id' => 1,
'title' => 'Client Site',
'meta' => [
'client_id' => 456,
'client_type' => 'premium',
],
] );
if ( true === $result ) {
// The site has been successfully initialized.
} #4 Changing arguments via a filter
The example automatically adds its own options for each new site before it is initialized.
add_filter( 'wp_initialize_site_args', 'my_change_site_initialization_args', 10, 3 );
function my_change_site_initialization_args( $args, $site, $network ) {
$args['options']['blog_public'] = 0;
$args['options']['timezone_string'] = 'Europe/Moscow';
$args['meta']['created_by_custom_flow'] = 1;
return $args;
}
Notes
- Global. wpdb.
$wpdbWordPress database abstraction object. - Global. WP_Roles.
$wp_rolesWordPress role management object.
Changelog
| Since 5.1.0 | Introduced. |