函数文档

get_blogaddress_by_domain()

💡 云策文档标注

概述

get_blogaddress_by_domain() 是一个已弃用的 WordPress 函数,用于根据域名和路径生成完整的站点 URL。它处理子域名安装和主博客的 URL 构建,并返回经过 sanitize_url() 处理的 URL。

关键要点

  • 函数已弃用:自 WordPress 3.7.0 版本起被标记为弃用,建议使用替代方法。
  • 参数:接受两个必需参数 $domain(字符串)和 $path(字符串)。
  • 返回值:返回一个字符串类型的完整 URL。
  • 功能逻辑:根据 is_subdomain_install() 判断安装类型,动态构建 URL,包括子域名和主博客的处理。
  • 相关函数:涉及 is_subdomain_install()、sanitize_url() 和 _deprecated_function() 等。

代码示例

function get_blogaddress_by_domain( $domain, $path ) {
    _deprecated_function( __FUNCTION__, '3.7.0' );

    if ( is_subdomain_install() ) {
        $url = "http://" . $domain.$path;
    } else {
        if ( $domain != $_SERVER['HTTP_HOST'] ) {
            $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
            $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
            // We're not installing the main blog.
            if ( 'www.' !== $blogname )
                $url .= $blogname . '/';
        } else { // Main blog.
            $url = 'http://' . $domain . $path;
        }
    }
    return sanitize_url( $url );
}

注意事项

  • 弃用状态:此函数自 3.7.0 版本起弃用,新代码中应避免使用,以防未来版本移除。
  • 安全处理:返回的 URL 通过 sanitize_url() 进行清理,确保安全使用。
  • 安装类型依赖:函数逻辑依赖于 is_subdomain_install() 的返回值,需确保多站点配置正确。

📄 原文内容

Get a full site URL, given a domain and a path.

Parameters

$domainstringrequired
$pathstringrequired

Return

string

Source

function get_blogaddress_by_domain( $domain, $path ) {
	_deprecated_function( __FUNCTION__, '3.7.0' );

	if ( is_subdomain_install() ) {
		$url = "http://" . $domain.$path;
	} else {
		if ( $domain != $_SERVER['HTTP_HOST'] ) {
			$blogname = substr( $domain, 0, strpos( $domain, '.' ) );
			$url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
			// We're not installing the main blog.
			if ( 'www.' !== $blogname )
				$url .= $blogname . '/';
		} else { // Main blog.
			$url = 'http://' . $domain . $path;
		}
	}
	return sanitize_url( $url );
}

Changelog

Version Description
3.7.0 Deprecated.
MU (3.0.0) Introduced.