If you’re building a plugin, you will probably get to the point where you would like to include files from the plugin folder. As building a plugin means the code should work no matter how the blog is configured, you should use a function that retrieves the path to the plugin folder, then hardcode only the name of the file that needs to be included. As usual, WordPress comes to the help with a special function that retrieves the path to the plugins folder: plugins_url.
Using plugins_url to retrieve the path to the plugins folder in WordPress
- you also hardcode the folder of the plugin (let’s say the plugin folder is wp-polls and we want the path to the css file): $url = plugins_url(); echo $url; wp_register_style( ‘wps_poll_Stylesheet’, $url . ‘/wps-poll/wps-poll.css’ ); Note: don’t change the plugin folder! Or if you do, remember the change the hardcoded part as well.
- You let plugins_url to retrieve the path using the $plugin parameter: $url = plugins_url(‘wps-poll.css’, __FILE__); echo $url; wp_register_style( ‘wps_poll_Stylesheet’, $url );
- If the files are in a subfolder you should use dirname(__FILE__): $url = plugins_url(‘wps-poll.css’, dirname(__FILE__)); echo $url; wp_register_style( ‘wps_poll_Stylesheet’, $url );