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?

6
  • 2
    4 spaces before a line formats as code. ctr-k for a selection. Commented Oct 18, 2010 at 0:04
  • 1
    Also, you're quoting Wordpress PHP functions right? You might want to state that. Commented Oct 18, 2010 at 0: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? Commented Oct 18, 2010 at 9: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. Commented Oct 18, 2010 at 9: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. Commented Oct 18, 2010 at 10:09

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I think wp_deregister_scriptis pretty close to what I need to do.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.