0

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?

connorcode
  • 2,341
  • 2
  • 8
  • 13
  • What about `__DIR__`? Something like 'wp_enqueue_style('my-widget-main-style', __DIR__ . '/css/style.css');'? – Stevish Oct 02 '20 at 20:24

2 Answers2

2

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__ );
Xhynk
  • 13,513
  • 8
  • 32
  • 69
  • Looks like this has it searching, but the directory begins in the includes file instead of the plugin's root: localhost:XXXXX/wp-content/plugins/my-plugin/includes/css/style.css?ver=5.5.1 – connorcode Oct 02 '20 at 21:12
  • Moved each folder into the includes folder, it worked! How would I be able to properly grab the root directory though, or is that improper in and of itself? – connorcode Oct 02 '20 at 21:14
  • 1
    My apologies, I missed the `/includes` in the line identifiers. Generally the including file is in the root (usually the main plugin file itself). But if that doesn't work for you, you can use the answer here: https://stackoverflow.com/questions/11094776/php-how-to-go-one-level-up-on-dirname-file. `__FILE__` pulls in the current file, the answer there gives your the ability to "go up one directory" from there. You could also define a variable for it in your main plugin file. Either way, just depends on what works best for you! – Xhynk Oct 02 '20 at 21:18
0

Here's how I did it in mine:

wp_enqueue_script( 'my-widget-main-style', plugins_url('/js/main.js', __FILE__) );
Stevish
  • 734
  • 5
  • 17