函数文档

wp_is_site_url_using_https()

💡 云策文档标注

概述

wp_is_site_url_using_https() 函数用于检查 WordPress 存储的当前站点 URL 是否使用 HTTPS。它通过直接访问 'siteurl' 选项并应用 'site_url' 过滤器来确保准确性。

关键要点

  • 函数返回布尔值:true 表示使用 HTTPS,false 表示未使用。
  • 使用 get_option('siteurl') 获取站点 URL,并通过 apply_filters('site_url', ...) 应用过滤器以避免当前请求方案的影响。
  • 依赖 wp_parse_url() 解析 URL 方案,确保跨 PHP 版本的一致性。
  • 在 WordPress 5.7.0 版本中引入。

代码示例

function wp_is_site_url_using_https() {
    /*
     * Use direct option access for 'siteurl' and manually run the 'site_url'
     * filter because `site_url()` will adjust the scheme based on what the
     * current request is using.
     */
    /** This filter is documented in wp-includes/link-template.php */
    $site_url = apply_filters( 'site_url', get_option( 'siteurl' ), '', null, null );

    return 'https' === wp_parse_url( $site_url, PHP_URL_SCHEME );
}

注意事项

  • 此函数检查的是 WordPress 应用文件(如 wp-blog-header.php 或 wp-admin/ 文件夹)可访问的 URL,而非前端用户访问的 URL。
  • 相关函数包括 wp_is_using_https() 和 WP_Site_Health::get_test_https_status(),用于更广泛的 HTTPS 检测。

📄 原文内容

Checks whether the current site’s URL where WordPress is stored is using HTTPS.

Description

This checks the URL where WordPress application files (e.g. wp-blog-header.php or the wp-admin/ folder) are accessible.

See also

Return

bool True if using HTTPS, false otherwise.

Source

function wp_is_site_url_using_https() {
	/*
	 * Use direct option access for 'siteurl' and manually run the 'site_url'
	 * filter because `site_url()` will adjust the scheme based on what the
	 * current request is using.
	 */
	/** This filter is documented in wp-includes/link-template.php */
	$site_url = apply_filters( 'site_url', get_option( 'siteurl' ), '', null, null );

	return 'https' === wp_parse_url( $site_url, PHP_URL_SCHEME );
}

Hooks

apply_filters( ‘site_url’, string $url, string $path, string|null $scheme, int|null $blog_id )

Filters the site URL.

Changelog

Version Description
5.7.0 Introduced.