从其网址获取博客的数字ID。
原型
get_blog_id_from_url( string $domain, string $path = '/' )
描述
在像example.com/blog1/这样的子目录安装中,$ domain将是根“example.com”,$ path是子目录’/ blog1 /’。对于像blog1.example.com这样的子域名,$ domain是’blog1.example.com’,$ path是’/’。
参数
$domain
(string)
(Required)
$path
(string)
(Optional)
子域安装不需要。
返回值
(int)
如果没有找到博客,则为0,否则为匹配博客的ID
源文件
路径:wp-includes/ms-functions.php
<?php
...
function get_blog_id_from_url( $domain, $path = '/' ) {
$domain = strtolower( $domain );
$path = strtolower( $path );
$id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
if ( $id == -1 ) // blog does not exist
return 0;
elseif ( $id )
return (int) $id;
$args = array(
'domain' => $domain,
'path' => $path,
'fields' => 'ids',
'number' => 1,
);
$result = get_sites( $args );
$id = array_shift( $result );
if ( ! $id ) {
wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
return 0;
}
wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
return $id;
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/get_blog_id_from_url/