plugin_dir_url()WP 2.8.0

Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.

No Hooks.

Return

String. the URL path of the directory that contains the plugin.

Usage

plugin_dir_url( $file );
$file(string) (required)
The filename of the plugin (__FILE__).

Examples

0

#1 Get URL to the plugin directory

Suppose in the plugin main file, which is located in the folder /wp-content/plugins/my_plugin we call the function as follows:

echo plugin_dir_url( __FILE__ );

// This will display on the screen:
// http://example.com/wp-content/plugins/my_plugin/
0

#2 Register plugin CSS style file

add_action( 'wp_enqueue_scripts', 'myplugin_scripts' );

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

Changelog

Since 2.8.0 Introduced.

Code of plugin_dir_url() WP 6.0.1

function plugin_dir_url( $file ) {
	return trailingslashit( plugins_url( '', $file ) );
}