domain_exists()
云策文档标注
概述
domain_exists() 函数用于检查站点名称(域名和路径组合)是否已被占用,确保在新建站点注册过程中名称的唯一性。它返回站点ID或null,并支持多网络环境。
关键要点
- 检查站点名称是否已存在,基于网络设置(子域名或子目录路径)。
- 接受三个参数:$domain(域名,必需)、$path(路径,必需)、$network_id(网络ID,可选,默认为1)。
- 返回 int|null:如果站点名称存在则返回站点ID,否则返回null。
- 内部使用 get_sites() 查询,并应用 'domain_exists' 过滤器钩子。
- 主要用于新站点注册流程,如 wpmu_create_blog() 和 wpmu_validate_blog_signup()。
代码示例
function domain_exists( $domain, $path, $network_id = 1 ) {
$path = trailingslashit( $path );
$args = array(
'network_id' => $network_id,
'domain' => $domain,
'path' => $path,
'fields' => 'ids',
'number' => 1,
'update_site_meta_cache' => false,
);
$result = get_sites( $args );
$result = array_shift( $result );
return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
}注意事项
- 路径参数会自动添加尾部斜杠(使用 trailingslashit())。
- 在多网络安装中,$network_id 参数才相关,默认值为1。
- 函数自 WordPress MU 3.0.0 版本引入。
原文内容
Checks whether a site name is already taken.
Description
The name is the site’s subdomain or the site’s subdirectory path depending on the network settings.
Used during the new site registration process to ensure that each site name is unique.
Parameters
$domainstringrequired-
The domain to be checked.
$pathstringrequired-
The path to be checked.
$network_idintoptional-
Network ID. Only relevant on multi-network installations.
Default:
1
Source
function domain_exists( $domain, $path, $network_id = 1 ) {
$path = trailingslashit( $path );
$args = array(
'network_id' => $network_id,
'domain' => $domain,
'path' => $path,
'fields' => 'ids',
'number' => 1,
'update_site_meta_cache' => false,
);
$result = get_sites( $args );
$result = array_shift( $result );
/**
* Filters whether a site name is taken.
*
* The name is the site's subdomain or the site's subdirectory
* path depending on the network settings.
*
* @since 3.5.0
*
* @param int|null $result The site ID if the site name exists, null otherwise.
* @param string $domain Domain to be checked.
* @param string $path Path to be checked.
* @param int $network_id Network ID. Only relevant on multi-network installations.
*/
return apply_filters( 'domain_exists', $result, $domain, $path, $network_id );
}
Hooks
- apply_filters( ‘domain_exists’, int|null $result, string $domain, string $path, int $network_id )
-
Filters whether a site name is taken.
Changelog
| Version | Description |
|---|---|
| MU (3.0.0) | Introduced. |