1

In my WordPress widget I use the following code:

wp_register_script('jquery-ui.widget', plugins_url('assets/js/jquery-ui-1.9.2.widget.js', dirname( __FILE__ )));

Unfortunately, the code gives me an invalid URL which is mixed with my system path:

http://test.dev/wp-content/plugins/C:/projects/wordpress/plugins/assets/js/jquery-ui-1.9.2.widget.js?ver=3.6

Is this problem maybe related to the fact that I have a local WordPress installation running on a Windows computer (with Apache 2)?

1 Answer 1

3

Short Answer

  1. jquery-ui.widget is one of standard scripts already included and registered in WP. You don't need to register it, just enqueue.
  2. In your code what is wrong is the dirname function inside plugin_url. Leave only __FILE__ as second argument of plugins_url

Long Answer: More Info

jquery-ui.widget is one of standard wordpress scripts so, in most cases, you don't have to/you don't need to use file uploaded by yourself, but just using the copy in wp installation.

In this case you don't need to register the script, because it is already registered, you have only to enqueue using wp_enqueue_script('jquery-ui-widget').

This will enqueue correctly the version of the script that comes with Wordpress. In current version of WP (3.6), 'jqueri-ui widget' is in version '1.10.3'.

Have you some problem with this version? Try to solve them... also because including your older version not assure you will not have problems: if another plugin or theme include WP version of the same script I let you the pleasure to imagine what happen.

If we forget you are tring to include one of WP standard script, the problem that your code has, is located in the plugins_url function.

Doc for second argument of plugins_url in the codex is not the clearest I've ever seen:

$plugin - Path under the plugins directory of which parent directory you want the $path to be relative to.

Is not clear enough if we need to pass directory path (just like you do) or the full path (with file name).

But several lines below in same codex page we read: The plugins_url() function is commonly used in a plugin file. Passing the __FILE__ PHP magic constant in the place of $plugin parameter makes the $path relative to the parent directory of that file

So, now we understand that we have to use function like so:

wp_register_script('jquery-ui.widget', plugins_url('assets/js/jquery-ui-1.9.2.widget.js', __FILE__ )); // without dirname( )

But notice that to make this code work, it must be in a file located in root directory of plugin, so best solution is put it in the main plugin file. Reason is simple: __FILE__ PHP magic constant contain always the full path of the file in which is written, even if this file is required or included in another. So if you put this code in a file placed in a inner plugin directory, it doesn't work for the scope.

Note also that 'assets/js/jquery-ui-1.9.2.widget.js' must be relative to the main plugin file, so 'assets' must be a first level subfolder of plugin root.

Hope it helps you out.

2
  • Hi G.M., thank you for this very detailed answer. This is really commendable! I have tried it without "dirname" but that has not helped. I think it is probably because my files are present as symlinks and __ FILE__ can not handle this?! Maybe this helps: wordpress.stackexchange.com/questions/102681/… Aug 5, 2013 at 20:11
  • What in the answer you link is the same of my code if you run wp_register_script from the main plugin file, as i suggest you in my answer. If you call the function in any other file follow answer you link. For debug assign FILE to a variable and var_dump it...
    – gmazzap
    Aug 6, 2013 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.