wpmu_create_blog()
云策文档标注
概述
wpmu_create_blog() 函数用于在 WordPress 多站点网络中创建新站点,适用于用户自助注册或超级管理员手动创建场景。它处理域名、路径、标题等参数,并返回站点 ID 或 WP_Error 对象。
关键要点
- 触发时机:用户自助注册新站点或超级管理员创建新站点时运行,可通过 'wpmu_new_blog' Hook 影响所有新站点。
- 参数说明:必需参数包括 $domain(域名)、$path(路径)、$title(标题)、$user_id(管理员用户 ID);可选参数 $options(初始站点选项数组)和 $network_id(网络 ID,默认为 1)。
- 返回值:成功时返回新站点 ID,失败时返回 WP_Error 对象。
- 注意事项:在子目录安装中,$domain 与主站点相同,$path 为子目录名;在子域名安装中,$domain 为新子域名加根域名,$path 为 '/'。
- 内部处理:检查域名是否已存在,设置站点数据和初始化数据,调用 wp_insert_site() 插入站点,并更新缓存。
代码示例
$site_id = wpmu_create_blog(
$domain,
$path,
$title,
$user_id,
$options,
get_current_network_id()
);注意事项
- $network_id 参数默认值为 1,但实际网络 ID 可能不同,建议使用 get_current_network_id() 函数以避免在错误网络上创建站点。
- $options 数组可包含状态键(如 'public'、'archived')来更新站点状态,或其他键值对来设置初始选项。
原文内容
Creates a site.
Description
This function runs when a user self-registers a new site as well as when a Super Admin creates a new site. Hook to ‘wpmu_new_blog’ for events that should affect all new sites.
On subdirectory installations, $domain is the same as the main site’s domain, and the path is the subdirectory name (eg ‘example.com’ and ‘/blog1/’). On subdomain installations, $domain is the new subdomain + root domain (eg ‘blog1.example.com’), and $path is ‘/’.
Parameters
$domainstringrequired-
The new site’s domain.
$pathstringrequired-
The new site’s path.
$titlestringrequired-
The new site’s title.
$user_idintrequired-
The user ID of the new site’s admin.
$optionsarrayoptional-
Array of key=>value pairs used to set initial site options.
If valid status keys are included ('public','archived','mature','spam','deleted', or'lang_id') the given site status(es) will be updated. Otherwise, keys and values will be used to set options for the new site.Default:
array() $network_idintoptional-
Network ID. Only relevant on multi-network installations.
Default:
1
Source
function wpmu_create_blog( $domain, $path, $title, $user_id, $options = array(), $network_id = 1 ) {
$defaults = array(
'public' => 0,
);
$options = wp_parse_args( $options, $defaults );
$title = strip_tags( $title );
$user_id = (int) $user_id;
// Check if the domain has been used already. We should return an error message.
if ( domain_exists( $domain, $path, $network_id ) ) {
return new WP_Error( 'blog_taken', __( 'Sorry, that site already exists!' ) );
}
if ( ! wp_installing() ) {
wp_installing( true );
}
$allowed_data_fields = array( 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
$site_data = array_merge(
array(
'domain' => $domain,
'path' => $path,
'network_id' => $network_id,
),
array_intersect_key( $options, array_flip( $allowed_data_fields ) )
);
// Data to pass to wp_initialize_site().
$site_initialization_data = array(
'title' => $title,
'user_id' => $user_id,
'options' => array_diff_key( $options, array_flip( $allowed_data_fields ) ),
);
$blog_id = wp_insert_site( array_merge( $site_data, $site_initialization_data ) );
if ( is_wp_error( $blog_id ) ) {
return $blog_id;
}
wp_cache_set_sites_last_changed();
return $blog_id;
}
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |
Skip to note 2 content
Ramon Ahnert
It’s important to note that the default value of the
$network_idparameter is 1. However, your network ID might be different.For this reason, it’s advisable to use get_current_network_id() function to prevent the creation of sites on the wrong network.