1. 首页
  2. WordPress 函数手册

plugins_url()

plugins_url( string $path = , string $plugin =  )

检索plugins或mu plugins目录中的URL。
Retrieves a URL within the plugins or mu-plugins directory.

目录锚点:#说明#参数#返回#源码#笔记


说明(Description)

如果未提供参数,则默认为插件目录URL。


参数(Parameters)

参数 类型 必填 说明
$path (string) 可选 附加到URL结尾的路径,如果提供了$plugin,则包括相对目录。
$plugin (string) 可选 插件或mu插件中文件的完整路径。URL将相对于其目录。通常这是通过将文件作为参数传递来完成的。

返回(Return)

(string)附加了可选路径的插件URL链接。


源码(Source)

/**
 * Retrieve a URL within the plugins or mu-plugins directory.
 *
 * Defaults to the plugins directory URL if no arguments are supplied.
 *
 * @since 2.6.0
 *
 * @param  string $path   Optional. Extra path appended to the end of the URL, including
 *                        the relative directory if $plugin is supplied. Default empty.
 * @param  string $plugin Optional. 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.
 * @return string Plugins URL link with optional paths appended.
*/
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) && 0 === strpos($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, '/');

	/**
	 * Filter 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 );
}
更新版本 源码位置 使用 被使用
2.6.0 wp-includes/link-template.php:3392 3 5

笔记(Notes)

常用
默认用法

plugins_url() 为WP2原创文章,链接:https://www.wp2.cn/functions/plugins_url/

发表评论

您的电子邮箱地址不会被公开。