plugins_url()
云策文档标注
概述
plugins_url() 函数用于获取插件或 mu-plugins 目录的绝对 URL,支持附加路径和相对文件路径。它通过内部常量 WP_PLUGIN_URL 或 WPMU_PLUGIN_URL 构建 URL,并应用 'plugins_url' 过滤器。关键要点
- 函数返回插件或 mu-plugins 目录的 URL,可选附加路径或基于指定文件相对路径。
- 参数 $path 为附加路径字符串,$plugin 为插件文件路径,常用 __FILE__ 使 $path 相对当前文件父目录。
- 内部使用 WP_PLUGIN_URL 或 WPMU_PLUGIN_URL 常量,并通过 apply_filters() 应用 'plugins_url' 过滤器。
- 建议在 'init' 或 'admin_init' 等 Hook 中调用,避免全局上下文,以确保过滤器生效。
- 函数处理路径规范化、URL 方案设置,并返回过滤后的 URL 字符串。
代码示例
// 基本用法:获取插件目录 URL
$plugins_url = plugins_url();
// 常见用法:使用 __FILE__ 使路径相对当前文件父目录
echo '<img src="' . plugins_url( 'images/icon.png', __FILE__ ) . '" />';
// 嵌套子目录时使用 dirname()
echo '<img src="' . plugins_url( 'images/icon.png', dirname( __FILE__ ) ) . '" />';
注意事项
- 避免在插件文件的全局上下文中直接调用 plugins_url(),应通过 Hook 调用以确保过滤器可被其他插件修改。
- mu-plugins 由于运行顺序较早,可以在全局上下文中过滤 plugins_url()。
- 不推荐直接使用 WordPress 内部常量如 WP_PLUGIN_URL,应通过此函数获取 URL。
原文内容
Retrieves a URL within the plugins or mu-plugins directory.
Description
Defaults to the plugins directory URL if no arguments are supplied.
Parameters
$pathstringoptional-
Extra path appended to the end of the URL, including the relative directory if $plugin is supplied. Default empty.
$pluginstringoptional-
A full path to a file inside a plugin or mu-plugin.
The URL will be relative to its directory. Default empty.
Typically this is done by passing__FILE__as the argument.
Source
function plugins_url( $path = '', $plugin = '' ) {
$path = wp_normalize_path( $path );
$plugin = wp_normalize_path( $plugin );
$mu_plugin_dir = wp_normalize_path( WPMU_PLUGIN_DIR );
if ( ! empty( $plugin ) && str_starts_with( $plugin, $mu_plugin_dir ) ) {
$url = WPMU_PLUGIN_URL;
} else {
$url = WP_PLUGIN_URL;
}
$url = set_url_scheme( $url );
if ( ! empty( $plugin ) && is_string( $plugin ) ) {
$folder = dirname( plugin_basename( $plugin ) );
if ( '.' !== $folder ) {
$url .= '/' . ltrim( $folder, '/' );
}
}
if ( $path && is_string( $path ) ) {
$url .= '/' . ltrim( $path, '/' );
}
/**
* Filters the URL to the plugins directory.
*
* @since 2.8.0
*
* @param string $url The complete URL to the plugins directory including scheme and path.
* @param string $path Path relative to the URL to the plugins directory. Blank string
* if no path is specified.
* @param string $plugin The plugin file path to be relative to. Blank string if no plugin
* is specified.
*/
return apply_filters( 'plugins_url', $url, $path, $plugin );
}
Hooks
- apply_filters( ‘plugins_url’, string $url, string $path, string $plugin )
-
Filters the URL to the plugins directory.
Changelog
| Version | Description |
|---|---|
| 2.6.0 | Introduced. |
Skip to note 3 content
Codex
Common Usage
The
plugins_url()function is commonly used in a plugin file. Passing the__FILE__PHP magic constant in the place of the$pluginparameter makes the$pathrelative to the parent directory of that file:echo '<img src="' . esc_url( plugins_url( 'images/wordpress.png', __FILE__ ) ) . '" > ';The above might output this HTML markup:
<img src="http://www.example.com/wp-content/plugins/my-plugin/images/wordpress.png">.If you are using the
plugins_url()function in a file that is nested inside a subdirectory of your plugin directory, you should use PHP’sdirname()function:echo '<img src="' . esc_url( plugins_url( 'images/wordpress.png', dirname(__FILE__) ) ) . '" > ';The above might output this HTML markup:
<img src="http://www.example.com/wp-content/plugins/images/wordpress.png">.Skip to note 4 content
Codex
Default Usage
The
$plugins_urlvariable will equal to the absolute URL to thepluginsormu-pluginsdirectory, e.g. “http://www.example.com/wp-content/plugins”.