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)?
Short Answer
jquery-ui.widget
is one of standard scripts already included and registered in WP. You don’t need to register it, just enqueue.dirname
function insideplugin_url
. Leave only__FILE__
as second argument ofplugins_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 fileSo, now we understand that we have to use function like so:
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.