# wp_initialize_site()

URL: https://chugunov.pro/api-wordpress/functions/wp_initialize_site/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 5.1.0.

## Сигнатура

```php
wp_initialize_site( int|WP_Site $site_id, array $args = array() ): true|WP_Error
```

## Описание

Этот процесс включает создание таблиц базы данных сайта и заполнение их значениями по умолчанию.

## Параметры

- `$site_id` `int|WP_Site` — обязательный. ID или объект сайта.
- `$args` `array` — необязательный, по умолчанию `array()`. Аргументы для изменения поведения инициализации.
  
  user_id intОбязательный. ID пользователя-администратора сайта.
  
  title stringНазвание сайта. По умолчанию 'Site %d', где %d — ID сайта.
  
  options arrayПользовательские пары опций $key => $value.
  
  meta arrayПользовательские пары метаданных сайта $key => $value.

## Возвращаемое значение

`true|WP_Error`

## Исходный код

Файл: `wp-includes/ms-site.php:653`

```php
function wp_initialize_site( $site_id, array $args = array() ) {
	global $wpdb, $wp_roles;

	if ( empty( $site_id ) ) {
		return new WP_Error( 'site_empty_id', __( 'Site ID must not be empty.' ) );
	}

	$site = get_site( $site_id );
	if ( ! $site ) {
		return new WP_Error( 'site_invalid_id', __( 'Site with the ID does not exist.' ) );
	}

	if ( wp_is_site_initialized( $site ) ) {
		return new WP_Error( 'site_already_initialized', __( 'The site appears to be already initialized.' ) );
	}

	$network = get_network( $site->network_id );
	if ( ! $network ) {
		$network = get_network();
	}

	$args = wp_parse_args(
		$args,
		array(
			'user_id' => 0,
			/* translators: %d: Site ID. */
			'title'   => sprintf( __( 'Site %d' ), $site->id ),
			'options' => array(),
			'meta'    => array(),
		)
	);

	/**
	 * Filters the arguments for initializing a site.
	 *
	 * @since 5.1.0
	 *
	 * @param array      $args    Arguments to modify the initialization behavior.
	 * @param WP_Site    $site    Site that is being initialized.
	 * @param WP_Network $network Network that the site belongs to.
	 */
	$args = apply_filters( 'wp_initialize_site_args', $args, $site, $network );

	$orig_installing = wp_installing();
	if ( ! $orig_installing ) {
		wp_installing( true );
	}

	$switch = false;
	if ( get_current_blog_id() !== $site->id ) {
		$switch = true;
		switch_to_blog( $site->id );
	}

	require_once ABSPATH . 'wp-admin/includes/upgrade.php';

	// Set up the database tables.
	make_db_current_silent( 'blog' );

	$home_scheme    = 'http';
	$siteurl_scheme = 'http';
	if ( ! is_subdomain_install() ) {
		if ( 'https' === parse_url( get_home_url( $network->site_id ), PHP_URL_SCHEME ) ) {
			$home_scheme = 'https';
		}
		if ( 'https' === parse_url( get_network_option( $network->id, 'siteurl' ), PHP_URL_SCHEME ) ) {
			$siteurl_scheme = 'https';
		}
	}

	// Populate the site's options.
	populate_options(
		array_merge(
			array(
				'home'        => untrailingslashit( $home_scheme . '://' . $site->domain . $site->path ),
				'siteurl'     => untrailingslashit( $siteurl_scheme . '://' . $site->domain . $site->path ),
				'blogname'    => wp_unslash( $args['title'] ),
				'admin_email' => '',
				'upload_path' => get_network_option( $network->id, 'ms_files_rewriting' ) ? UPLOADBLOGSDIR . "/{$site->id}/files" : get_blog_option( $network->site_id, 'upload_path' ),
				'blog_public' => (int) $site->public,
				'WPLANG'      => get_network_option( $network->id, 'WPLANG' ),
			),
			$args['options']
		)
	);

	// Clean blog cache after populating options.
	clean_blog_cache( $site );

	// Populate the site's roles.
	populate_roles();
	$wp_roles = new WP_Roles();

	// Populate metadata for the site.
	populate_site_meta( $site->id, $args['meta'] );

	// Remove all permissions that may exist for the site.
	$table_prefix = $wpdb->get_blog_prefix();
	delete_metadata( 'user', 0, $table_prefix . 'user_level', null, true );   // Delete all.
	delete_metadata( 'user', 0, $table_prefix . 'capabilities', null, true ); // Delete all.

	// Install default site content.
	wp_install_defaults( $args['user_id'] );

	// Set the site administrator.
	add_user_to_blog( $site->id, $args['user_id'], 'administrator' );
	if ( ! user_can( $args['user_id'], 'manage_network' ) && ! get_user_meta( $args['user_id'], 'primary_blog', true ) ) {
		update_user_meta( $args['user_id'], 'primary_blog', $site->id );
	}

	if ( $switch ) {
		restore_current_blog();
	}

	wp_installing( $orig_installing );

	return true;
}
```

## История изменений

- 5.1.0 — Introduced.

## Связанные

Использует: [`wp_is_site_initialized`](https://chugunov.pro/api-wordpress/functions/wp_is_site_initialized/), [`populate_site_meta`](https://chugunov.pro/api-wordpress/functions/populate_site_meta/), [`get_network`](https://chugunov.pro/api-wordpress/functions/get_network/), [`get_site`](https://chugunov.pro/api-wordpress/functions/get_site/), [`wp_installing`](https://chugunov.pro/api-wordpress/functions/wp_installing/), [`get_network_option`](https://chugunov.pro/api-wordpress/functions/get_network_option/), [`populate_roles`](https://chugunov.pro/api-wordpress/functions/populate_roles/), [`populate_options`](https://chugunov.pro/api-wordpress/functions/populate_options/), [`make_db_current_silent`](https://chugunov.pro/api-wordpress/functions/make_db_current_silent/), [`wp_install_defaults`](https://chugunov.pro/api-wordpress/functions/wp_install_defaults/), [`user_can`](https://chugunov.pro/api-wordpress/functions/user_can/), `WP_Roles::__construct`, [`untrailingslashit`](https://chugunov.pro/api-wordpress/functions/untrailingslashit/), [`get_home_url`](https://chugunov.pro/api-wordpress/functions/get_home_url/), [`get_user_meta`](https://chugunov.pro/api-wordpress/functions/get_user_meta/), [`update_user_meta`](https://chugunov.pro/api-wordpress/functions/update_user_meta/), [`add_user_to_blog`](https://chugunov.pro/api-wordpress/functions/add_user_to_blog/), [`is_subdomain_install`](https://chugunov.pro/api-wordpress/functions/is_subdomain_install/), [`switch_to_blog`](https://chugunov.pro/api-wordpress/functions/switch_to_blog/), [`restore_current_blog`](https://chugunov.pro/api-wordpress/functions/restore_current_blog/), [`get_blog_option`](https://chugunov.pro/api-wordpress/functions/get_blog_option/), [`clean_blog_cache`](https://chugunov.pro/api-wordpress/functions/clean_blog_cache/), `wpdb::get_blog_prefix`, [`delete_metadata`](https://chugunov.pro/api-wordpress/functions/delete_metadata/), [`__`](https://chugunov.pro/api-wordpress/functions/__/), [`wp_unslash`](https://chugunov.pro/api-wordpress/functions/wp_unslash/), [`get_current_blog_id`](https://chugunov.pro/api-wordpress/functions/get_current_blog_id/), [`wp_parse_args`](https://chugunov.pro/api-wordpress/functions/wp_parse_args/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), `WP_Error::__construct`.

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/wp_initialize_site/
