函数文档

content_url()

💡 云策文档标注

概述

content_url() 函数用于获取 WordPress 内容目录的 URL,支持可选路径参数。开发者应使用此函数而非硬编码路径,以确保兼容自定义内容目录配置。

关键要点

  • 函数返回内容目录的 URL,可附加相对路径参数
  • 参数 $path 为可选字符串,默认为空,用于指定相对于内容目录的路径
  • 通过 apply_filters('content_url', $url, $path) 钩子允许过滤返回的 URL
  • 始终使用 content_url() 引用内容目录,避免硬编码 /wp-content 路径
  • 内容目录位置和名称可通过定义常量(如 WP_CONTENT_URL)自定义

代码示例

// 获取内容目录 URL
echo content_url(); // 输出: http://www.example.com/wp-content (无尾部斜杠)

// 获取内容目录下 plugins 文件夹的 URL
echo content_url('/plugins'); // 输出: https://example.com/wp-content/plugins

注意事项

  • 如需获取内容目录的物理路径,请使用 WP_CONTENT_DIR 常量而非此函数
  • 函数自 WordPress 2.6.0 版本引入,适用于插件、主题和上传文件夹的 URL 引用

📄 原文内容

Retrieves the URL to the content directory.

Parameters

$pathstringoptional
Path relative to the content URL. Default empty.

Return

string Content URL link with optional path appended.

Source

function content_url( $path = '' ) {
	$url = set_url_scheme( WP_CONTENT_URL );

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

	/**
	 * Filters the URL to the content directory.
	 *
	 * @since 2.8.0
	 *
	 * @param string $url  The complete URL to the content directory including scheme and path.
	 * @param string $path Path relative to the URL to the content directory. Blank string
	 *                     if no path is specified.
	 */
	return apply_filters( 'content_url', $url, $path );
}

Hooks

apply_filters( ‘content_url’, string $url, string $path )

Filters the URL to the content directory.

Changelog

Version Description
2.6.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    This function is useful when you need to reference files and folders inside the content directory, which includes the plugins, themes and uploads folders. By default, it is the /wp-content directory. However, users are actually allowed to change the name of this directory and place it anywhere they want, as stated here: https://developer.wordpress.org/plugins/plugin-basics/determining-plugin-and-content-directories/

    Always use the content_url() function to reference the content directory. Never hardcode this directory, assuming that it’s the /wp-content directory.

    Moving the content directory or changing its name requires defining some constants in wp-config.php. It’s not the topic of this note. Below you’ll find some examples of the return value of this function.

    If your content folder is in its default location and has the default name:

    • The URL of the content directory:
    • echo content_url(); // -> <a href="https://example.com/wp-content" rel="nofollow ugc">https://example.com/wp-content</a> (without a trailing slash) 
    • The URL of the plugins folder inside the content directory:
    • echo content_url('/plugins'); // -> <a href="https://example.com/wp-content/plugins" rel="nofollow ugc">https://example.com/wp-content/plugins</a> 

    If your content folder was renamed, for example, to ‘assets‘:

    • The URL of the content directory:
    • echo content_url(); // -> <a href="https://example.com/assets" rel="nofollow ugc">https://example.com/assets</a> 
    • The URL of the plugins folder inside the content directory:
    • echo content_url('/plugins'); // -> <a href="https://example.com/assets/plugins" rel="nofollow ugc">https://example.com/assets/plugins</a>