Get Current Plugin URL
wp_enqueue_style('stylsheet', 'http://www.example.com/stylesheet.css' );
wp_enqueue_style('stylsheet', 'http://www.example.com/wp-content/plugins/example-plugin/css/stylesheet.css' );
This will work fine but it's not best practice, for example you can only use this on the one domain, you can't move this plugin inside the mu-plugins folder and you can't change the name of the plugin folder.
So to get around these problems WordPress comes with a very useful function called plugins_url(), you give this function the filepath and it will return with the URL to this plugin. It is very useful to use when you need to include Savascript or CSS files in your plugin.
$url = plugins_url( $path, $plugin );
If you want to include a stylesheet from within a plugin the best way to do this is to use the global variable __FILE__ as the second parameter, this will always return the URL root of the plugin.
So if your stylesheet is in a css folder on the root of your plugin you will enqueue it by using the following code.
wp_enqueue_style('stylsheet', plugins_url( 'css/stylesheet.css', __FILE__ ) );
Learn how to code with Treehouse
- Learn projects with access to 1000+ videos
- Practice live with our Code Challenge Engine
- Get help in our members-only forums
- Access 100s of premium tutorials and downloadable content
- Members content consists of premium WordPress plugins
- CSS packages, jQuery packages, tutorial demo files and templates for 100s of web development tutorials
- In-depth development tutorials
- Priority tutorial requests
- No ads
- First month free!