plugin_dir_url( string $file )

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


Description Description


Parameters Parameters

$file

(string) (Required) The filename of the plugin (__FILE__).


Top ↑

Return Return

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


Top ↑

Source Source

File: wp-includes/plugin.php

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

Top ↑

Changelog Changelog

Changelog
Version Description
2.8.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Codex

    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
    
  2. Skip to note 2 content
    Contributed by 10Horizons

    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

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