函数文档

wp_guess_url()

💡 云策文档标注

概述

wp_guess_url() 函数用于猜测站点的 URL,主要处理移除 wp-admin 相关链接以返回非管理目录的 URL。它基于服务器环境变量和配置动态计算 URL。

关键要点

  • 函数返回一个字符串,表示猜测的站点 URL。
  • 如果定义了 WP_SITEURL 常量且非空,则直接使用该值。
  • 否则,通过分析 ABSPATH、$_SERVER['SCRIPT_FILENAME'] 和 $_SERVER['REQUEST_URI'] 等变量来构建 URL。
  • 处理不同请求场景,如访问 wp-admin、文件在 ABSPATH 内或外部。
  • 使用 is_ssl() 判断协议,并移除末尾斜杠。

代码示例

function wp_guess_url() {
    if ( defined( 'WP_SITEURL' ) && '' !== WP_SITEURL ) {
        $url = WP_SITEURL;
    } else {
        $abspath_fix         = str_replace( '', '/', ABSPATH );
        $script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] );

        // The request is for the admin.
        if ( str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) || str_contains( $_SERVER['REQUEST_URI'], 'wp-login.php' ) ) {
            $path = preg_replace( '#/(wp-admin/?.*|wp-login.php.*)#i', '', $_SERVER['REQUEST_URI'] );

            // The request is for a file in ABSPATH.
        } elseif ( $script_filename_dir . '/' === $abspath_fix ) {
            // Strip off any file/query params in the path.
            $path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] );

        } else {
            if ( str_contains( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) {
                // Request is hitting a file inside ABSPATH.
                $directory = str_replace( ABSPATH, '', $script_filename_dir );
                // Strip off the subdirectory, and any file/query params.
                $path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '', $_SERVER['REQUEST_URI'] );
            } elseif ( str_contains( $abspath_fix, $script_filename_dir ) ) {
                // Request is hitting a file above ABSPATH.
                $subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) );
                // Strip off any file/query params from the path, appending the subdirectory to the installation.
                $path = preg_replace( '#/[^/]*$#i', '', $_SERVER['REQUEST_URI'] ) . $subdirectory;
            } else {
                $path = $_SERVER['REQUEST_URI'];
            }
        }

        $schema = is_ssl() ? 'https://' : 'http://'; // set_url_scheme() is not defined yet.
        $url    = $schema . $_SERVER['HTTP_HOST'] . $path;
    }

    return rtrim( $url, '/' );
}

注意事项

  • 该函数在 WordPress 2.6.0 版本引入。
  • 主要用于安装和初始化过程,如 populate_options() 和 wp_install()。
  • 依赖服务器变量,确保 $_SERVER 正确设置以避免错误。

📄 原文内容

Guesses the URL for the site.

Description

Will remove wp-admin links to retrieve only return URLs not in the wp-admin directory.

Return

string The guessed URL.

Source

function wp_guess_url() {
	if ( defined( 'WP_SITEURL' ) && '' !== WP_SITEURL ) {
		$url = WP_SITEURL;
	} else {
		$abspath_fix         = str_replace( '\', '/', ABSPATH );
		$script_filename_dir = dirname( $_SERVER['SCRIPT_FILENAME'] );

		// The request is for the admin.
		if ( str_contains( $_SERVER['REQUEST_URI'], 'wp-admin' ) || str_contains( $_SERVER['REQUEST_URI'], 'wp-login.php' ) ) {
			$path = preg_replace( '#/(wp-admin/?.*|wp-login.php.*)#i', '', $_SERVER['REQUEST_URI'] );

			// The request is for a file in ABSPATH.
		} elseif ( $script_filename_dir . '/' === $abspath_fix ) {
			// Strip off any file/query params in the path.
			$path = preg_replace( '#/[^/]*$#i', '', $_SERVER['PHP_SELF'] );

		} else {
			if ( str_contains( $_SERVER['SCRIPT_FILENAME'], $abspath_fix ) ) {
				// Request is hitting a file inside ABSPATH.
				$directory = str_replace( ABSPATH, '', $script_filename_dir );
				// Strip off the subdirectory, and any file/query params.
				$path = preg_replace( '#/' . preg_quote( $directory, '#' ) . '/[^/]*$#i', '', $_SERVER['REQUEST_URI'] );
			} elseif ( str_contains( $abspath_fix, $script_filename_dir ) ) {
				// Request is hitting a file above ABSPATH.
				$subdirectory = substr( $abspath_fix, strpos( $abspath_fix, $script_filename_dir ) + strlen( $script_filename_dir ) );
				// Strip off any file/query params from the path, appending the subdirectory to the installation.
				$path = preg_replace( '#/[^/]*$#i', '', $_SERVER['REQUEST_URI'] ) . $subdirectory;
			} else {
				$path = $_SERVER['REQUEST_URI'];
			}
		}

		$schema = is_ssl() ? 'https://' : 'http://'; // set_url_scheme() is not defined yet.
		$url    = $schema . $_SERVER['HTTP_HOST'] . $path;
	}

	return rtrim( $url, '/' );
}

Changelog

Version Description
2.6.0 Introduced.