返回介绍

plugins_url()

发布于 2017-09-11 09:54:43 字数 4043 浏览 1949 评论 0 收藏 0

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

Retrieves a URL within the plugins or mu-plugins directory.


description

Defaults to the plugins directory URL if no arguments are supplied.


参数

$path
(string)
(Optional)
Extra path appended to the end of the URL, including the relative directory if $plugin is supplied.Default value: ''
$plugin
(string)
(Optional)
A full path to a file inside a plugin or mu-plugin. The URL will be relative to its directory. Typically this is done by passing __FILE__ as the argument.Default value: ''

返回值

(string) Plugins URL link with optional paths appended.


源代码

File: wp-includes/link-template.php

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, '/');

	/**
	 * 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 );
}

更新日志

Versiondescription
2.6.0Introduced.

相关函数

Uses

  • wp-includes/functions.php:wp_normalize_path()
  • wp-includes/link-template.php:set_url_scheme()
  • wp-includes/link-template.php:plugins_url
  • wp-includes/plugin.php:plugin_basename()
  • wp-includes/plugin.php:apply_filters()

Used By

  • wp-includes/theme.php:get_theme_root_uri()
  • wp-includes/plugin.php:plugin_dir_url()

使用举例

通用方法

The plugins_url() function is commonly used in a plugin file. Passing the __FILE__ PHP magic constant in the place of the $plugin parameter makes the $path relative 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’s dirname() 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">.

默认使用

$plugins_url = plugins_url();

The $plugins_url variable will equal to the absolute URL to the plugins or mu-plugins directory, e.g. “http://www.example.com/wp-content/plugins”.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文