plugin_dir_url( string $file ): string
In this article
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 ) );
}
Related
| Uses | Description |
|---|---|
plugins_url() wp-includes/link-template.php | Retrieves a URL within the plugins or mu-plugins directory. |
trailingslashit() wp-includes/formatting.php | Appends a trailing slash. |
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
User Contributed Notes
-
Skip to note 4 content You must log in to vote on the helpfulness of this note Vote results for this note: 17You must log in to vote on the helpfulness of this note-
Let’s say current URL is: http://example.com/wp-content/plugins/my-plugin/includes/pages/
echo plugin_dir_url( __FILE__ ).'images/placeholder.png';will output http://example.com/wp-content/plugins/my-plugin/includes/pages/images/placeholder.pngecho plugin_dir_url( __DIR__ ).'images/placeholder.png';will output http://example.com/wp-content/plugins/my-plugin/includes/images/placeholder.png Which is still not the plugin directory, but only one directory above. I’m still searching for a logical function that returns the root of our plugins.
-
-
Skip to note 5 content You must log in to vote on the helpfulness of this note Vote results for this note: 3You must log in to vote on the helpfulness of this noteBasic 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 -
Skip to note 6 content You must log in to vote on the helpfulness of this note Vote results for this note: 2You must log in to vote on the helpfulness of this noteplugin_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');-
plugin_dir_url vs plugin_dir_path difference
plugin_dir_url( string $file )Use it to include your assets (images, css, js etc).
plugin_dir_path( string $file )Use it to include php files.
-
You must log in before being able to contribute a note or feedback.
Let’s say current URL is: http://example.com/wp-content/plugins/my-plugin/includes/
will output: http://example.com/wp-content/plugins/my-plugin/includes/images/placeholder.png
will output: http://example.com/wp-content/plugins/my-plugin/images/placeholder.png