plugin_dir_url()返回当前插件的目录URL

目录

描述

Gets the URL (with trailing slash) for the plugin __FILE__ passed in

plugin_dir_url() 描述

用法

<?php $url plugin_dir_url$file ); ?>

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/** * Include CSS file for MyPlugin. */function myplugin_scripts() {    wp_register_style( 'foo-styles',  plugin_dir_url( __FILE__ ) . 'assets/foo-styles.css' );    wp_enqueue_style( 'foo-styles' );}add_action( 'wp_enqueue_scripts', 'myplugin_scripts' );

Would echo:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
http://example.com/wp-content/plugins/my-plugin/assets/foo-styles.css

plugin_dir_url() 用法

参数

$file

(string) (必填) The filename of the plugin (__FILE__)

默认值: None

plugin_dir_url() 参数

返回值

(string

The URL of the directory that contains the plugin, including a trailing slash ("/")

plugin_dir_url() 返回值

历史

添加于 版本: 2.8

plugin_dir_url() 历史

源文件

plugin_dir_url() 函数的代码位于 wp-includes/plugin.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.
 *
 * @since 2.8.0
 *
 * @param string $file The filename of the plugin (__FILE__).
 * @return string the URL path of the directory that contains the plugin.
 */
function plugin_dir_url( $file ) {
	return trailingslashit( plugins_url( '', $file ) );
}

plugin_dir_url() 源文件

相关