plugin_dir_url()WP 2.8.0

Gets the URL of the folder (with a slash at the end) where the specified plugin file is located.

The function is created for convenient use of the construction:

trailingslashit( plugins_url( '', $file ) );

If you need to get the full path to the plugin file directory, not the URL, use a similar function plugin_dir_path().

Use get_template_directory_uri() when you need to get the theme URL.

No Hooks.

Returns

String. The URL of the file directory.

Usage

$url = plugin_dir_url( $file );
$file(string) (required)
The full path to the file. Usually uses the magic constant __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.

plugin_dir_url() code WP 6.9.1

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