plugin_dir_url( string $file ): string

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

Parameters

$filestringrequired
The filename of the plugin (__FILE__).

Return

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

Source

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

Changelog

VersionDescription
2.8.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Let’s say current URL is: http://example.com/wp-content/plugins/my-plugin/includes/

    echo plugin_dir_url( __FILE__ ).'images/placeholder.png';

    will output: http://example.com/wp-content/plugins/my-plugin/includes/images/placeholder.png

    echo plugin_dir_url( __DIR__ ).'images/placeholder.png';

    will output: http://example.com/wp-content/plugins/my-plugin/images/placeholder.png

  2. Skip to note 5 content

    Basic Example

    /**
     * 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:

    http://example.com/wp-content/plugins/my-plugin/assets/foo-styles.css
  3. Skip to note 6 content

    plugin_dir_url( __FILE__ ) == http://your-url.com/wp-content/plugins/your-plugin/

    function enqueue_scripts() {
    	wp_enqueue_script( 'custom-js', plugin_dir_url( __FILE__ ) . 'js/custom.js', array( 'jquery' ), '', true );
    	wp_enqueue_style( 'style-css', plugin_dir_url( __FILE__ ) . 'css/style.css' );
    }
    add_action( 'wp_enqueue_scripts', 'enqueue_scripts');
    
    function admin_enqueue_scripts() {
    	wp_enqueue_script( 'custom-js', plugin_dir_url( __FILE__ ) . 'js/custom.js', array( 'jquery' ), '', true );
    	wp_enqueue_style( 'style-css', plugin_dir_url( __FILE__ ) . 'css/style.css' );
    }
    add_action( 'admin_enqueue_scripts', 'admin_enqueue_scripts');

You must log in before being able to contribute a note or feedback.