plugins_url()获取插件所在的网址

目录

描述

Retrieves the absolute URL to the plugins or mu-plugins directory (without the trailing slash) or, when using the $path argument, to a specific file under that directory. You can either specify the $path argument as a hardcoded path relative to the plugins or mu-plugins directory, or conveniently pass __FILE__ as the second argument to make the $path relative to the parent directory of the current PHP script file.

plugins_url() 描述

用法

 <?php plugins_url$path$plugin ); ?> 

Default Usage

<?php $url plugins_url(); ?>

plugins_url() 用法

参数

$path

(string) (可选) Path to the plugin file of which URL you want to retrieve, relative to the plugins or mu-plugins directory or to $plugin if specified.

默认值: None

$plugin

(string) (可选) Path under the plugins or mu-plugins directory of which parent directory you want the $path to be relative to.

默认值: None

plugins_url() 参数

返回值

(string

Absolute URL to the plugins or mu-plugins directory (without the trailing slash) or optionally to a file under that directory.

plugins_url() 返回值

注意

  • 使用到 the WP_PLUGIN_URL or, in the case the $plugin path begins with the WPMU_PLUGIN_DIR path, the WPMU_PLUGIN_URL constant internally, to compose the resultant URL. Note that the direct usage of WordPress internal constants is not recommended.
  • 使用到 apply_filters() to apply "plugins_url" 过滤器s on the resultant URL, with the following line of code:
    /* ----------------------------------
     * wordpress之魂 © http://wphun.com
     * ---------------------------------- */
    return apply_filters( 'plugins_url', $url, $path, $plugin );
  • The plugins_url() function should not be called in the global context of plugins, but rather in a hook like "init" or "admin_init" to ensure that the "plugins_url" 过滤器s are already hooked at the time the function is called. This is vital for many site configurations to work, and if plugins_url() is called in the global context of a plugin file it cannot be 过滤器ed by other plugins (though mu-plugins are able to 过滤器 it because they run before any other plugins).

plugins_url() 注意

历史

  • 添加于 版本: 2.6.0

plugins_url() 历史

源文件

plugins_url() 函数的代码位于 wp-includes/link-template.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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 );
}

plugins_url() 源文件

相关