3

On my site, I load jQuery UI script using the following code:

wp_enqueue_script('nf-jquery-ui','http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js')

In my plugin I use the following code:

wp_enqueue_script('wp_filebrowser-jqueryui','http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js')

The result is that both these lines are added to my header - and loading a script twice is not very good.

Yes, I know I can remove it from either my plugin or my site. But the point is that anyone can download my plugin, and they might already be loading the jQuery UI script.

How can I avoid having a script being added twice?

Steven
  • 19,224
  • 47
  • 152
  • 257
  • 2
    4 spaces before a line formats as code. `ctr-k` for a selection. – Peter Ajtai Oct 18 '10 at 00:04
  • 1
    Also, you're quoting Wordpress PHP functions right? You might want to state that. – Peter Ajtai Oct 18 '10 at 00:05
  • @ Peter: It's less readable having 4 spaces thus getting a scrollbar. Using '' makes it easier to read in this case. - So having Wordpress in tags and using `wp_enqueue_script` is not enough pointers? – Steven Oct 18 '10 at 09:25
  • No, not everyone is familiar with wordpress / wordpress function names. I had to check the tags to confirm these were not jQuery functions. Personally I don’t first read tags then question, but the other way around. And for that, having the text state every useful info is a lot better than pruning information that the tags may provide; you’ll have to reread the question. – Kissaki Oct 18 '10 at 09:29
  • 1
    +1 Peter Ajtai, ctrl+k never knew that. I've been spacing huge amounts of code for ages. Feel so crap right now. – Josh Smeaton Oct 18 '10 at 10:09
  • @Kissaki & Josh: If you are familiar with WP and WP plugin development, you know that `wp_enqueue_script`is a WP function. And if you don't know, then you most likely do not have the required knowledge to help me solve this issue. – Steven Oct 18 '10 at 13:46

2 Answers2

2

You could deregister the first script by putting this line in your plugin...

wp_deregister_script( 'nf-jquery-ui');

before you call your 'enqueue script'

wp_enqueue_script('wp_filebrowser-jqueryui','http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js')

This should remove the first jQuery call on all pages that use your plugin.

bharat parmar
  • 302
  • 3
  • 14
awats
  • 447
  • 2
  • 5
0

The WP API states the first param is a handler. That one is probably used so a script is only inserted once (although I can not say for sure).

It also defines handlers for jQuery and jQuery UI which you should probably use. Those handlers are for included scripts and jQuery as well as jQuery-ui is listed there. If you want to update consider passing the version to the wp_-function or just replacing the jQuery script file that’s already in use / packaged with wp.

Kissaki
  • 8,810
  • 5
  • 40
  • 42