函数文档

includes_url()

💡 云策文档标注

概述

includes_url() 函数用于获取 WordPress 核心 includes 目录的 URL,支持附加相对路径和指定协议方案。该函数通过 site_url() 构建基础 URL,并应用 includes_url 过滤器进行自定义。

关键要点

  • 函数返回 includes 目录的 URL,可选附加路径和指定协议(如 'http'、'https' 或 'relative')。
  • 参数 $path 为字符串类型,可选,默认为空,用于指定相对于 includes URL 的路径。
  • 参数 $scheme 为字符串或 null,可选,默认为 null,用于设置 URL 的协议上下文。
  • 内部使用 site_url() 和 WPINC 常量构建 URL,并通过 apply_filters() 应用 includes_url 钩子进行过滤。
  • 相关函数包括 site_url() 和 apply_filters(),被多个核心功能如脚本注册、块资产获取等调用。

代码示例

$url = includes_url();
echo $url;

注意事项

  • 函数自 WordPress 2.6.0 引入,5.8.0 版本添加了 $scheme 参数支持。
  • 使用 includes_url 过滤器可以自定义返回的 URL,适用于主题或插件开发中的特定需求。

📄 原文内容

Retrieves the URL to the includes directory.

Parameters

$pathstringoptional
Path relative to the includes URL. Default empty.
$schemestring|nulloptional
Scheme to give the includes URL context. Accepts 'http', 'https', or 'relative'.

Default:null

Return

string Includes URL link with optional path appended.

Source

function includes_url( $path = '', $scheme = null ) {
	$url = site_url( '/' . WPINC . '/', $scheme );

	if ( $path && is_string( $path ) ) {
		$url .= ltrim( $path, '/' );
	}

	/**
	 * Filters the URL to the includes directory.
	 *
	 * @since 2.8.0
	 * @since 5.8.0 The `$scheme` parameter was added.
	 *
	 * @param string      $url    The complete URL to the includes directory including scheme and path.
	 * @param string      $path   Path relative to the URL to the wp-includes directory. Blank string
	 *                            if no path is specified.
	 * @param string|null $scheme Scheme to give the includes URL context. Accepts
	 *                            'http', 'https', 'relative', or null. Default null.
	 */
	return apply_filters( 'includes_url', $url, $path, $scheme );
}

Hooks

apply_filters( ‘includes_url’, string $url, string $path, string|null $scheme )

Filters the URL to the includes directory.

Changelog

Version Description
2.6.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    What it returns

    echo includes_url(); // <a href="https://" rel="ugc">https://</a>{domain}/wp-includes
    echo includes_url('', 'https'); // <a href="https://" rel="ugc">https://</a>{domain}/wp-includes
    echo includes_url('', 'http'); // <a href="http://" rel="ugc">http://</a>{domain}/wp-includes
    echo includes_url('', 'relative'); // /wp-includes/