I’ve submitted a widget to WordPress.org and received this guidance:
The way your plugin is referencing other files is not going to work with all setups of WordPress.
When you hardcode in paths like wp-content or your plugin folder name, or assume that everyone has WordPress in the root of their domain, you cause anyone using ‘Giving WordPress it’s own directory’ (a VERY common setup) to break. In addition, WordPress allows users to change the name of wp-content, so you would break anyone who chooses to do so.
Please review the following link and update your plugin accordingly. And don’t worry about supporting WordPress 2.x or lower. We don’t encourage it nor expect you to do so, so save yourself some time and energy.
Remember to make use of the FILE variable, in order than your plugin function properly in the real world.
Example(s) from your plugin:
my-widget/includes/my-widget-scripts.php:6: wp_enqueue_style('my-widget-main-style', plugins_url(). '/my-widget/css/style.css'); my-widget/includes/my-widget-scripts.php:8: wp_enqueue_script('my-widget-main-script', plugins_url(). '/my-widget/js/main.js');
I’ve followed the link and tried out some of the variations therein, but nothing has worked to replace the code I currently have (found at the bottom of the quote).
What should I replace it with that will be found up to standards for WordPress.org?
Advertisement
Answer
Take a look at the documentation for plugins_url()
. The first argument takes a $path
argument to a append to the URL. The second takes a full path to a plugin. if you pass the __FILE__
Magic Constant to it, it will be for your current plugin.
You should take advantage of both of those arguments:
wp_enqueue_style( 'my-widget-main-style', plugins_url( '/css/style.css', __FILE__ ); wp_enqueue_script( 'my-widget-main-script', plugins_url( '/js/main.js', __FILE__ );