Get the URL directory path (with trailing slash) for the plugin __FILE__ passed in.
Parameters
$file
stringrequired- The filename of the plugin (__FILE__).
Source
function plugin_dir_url( $file ) {
return trailingslashit( plugins_url( '', $file ) );
}
Changelog
Version | Description |
---|---|
2.8.0 | Introduced. |
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
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.Basic Example
Would echo:
plugin_dir_url( __FILE__ ) == http://your-url.com/wp-content/plugins/your-plugin/
Use it to include your assets (images, css, js etc).
Use it to include php files.