Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Trying to do the right thing here, and tying myself in a knot! So, in my header.php before wp_admin, I have:

wp_enqueue_script('commonfunctions', child_template_directory . '/script/commonfunctions.js', array('jquery', 'jquery-ui-draggable'), '1.0', true);

which works. Basically, I have a custom js file which needs jquery and jqueryui as dependencies. I since took 'jquery' out as it seems to be loaded twice irrespectively. I also had a function hooked into 'init' in functions.php, but that didn't seem to have any advantage?

I want to include the validate script from CDN as this will also be a dependency of my custom script. I thought I would register the script like so:

wp_register_script('validate', 'http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js', true);

and then add 'validate' to the dependencies array in enqueue, like so:

wp_enqueue_script('commonfunctions', child_template_directory . '/script/commonfunctions.js', array('jquery', 'jquery-ui-draggable', 'validate'), '1.0', true);

in that order, as jquery is also a dependency of validate. Phew! That doesn't work, I'm obviously misunderstanding something - please help me uncurl what I'm trying to do here...

share|improve this question

2 Answers 2

I since took 'jquery' out as it seems to be loaded twice irrespectively.

This should not happen and is likely cause by some other (poor) code active.

You have wrong true in validate registration - after source comes dependencies array and true is meaningless there.

Otherwise this seems fine in general. Works from quick test. Could you describe more precisely what exactly and how "doesn't work"?

share|improve this answer
    
Ah yes, I spotted that. Presumably, it can take null as an argument? –  Richard Lane Aug 1 '11 at 11:40
    
It may have been all that was wrong. I now have: wp_register_script('validate', 'http://ajax.microsoft.com/ajax/jquery.validate/1.8.1/jquery.validate.min.js', array('jquery'), null, true); wp_enqueue_script('commonfunctions', child_template_directory . '/script/commonfunctions.js', array('jquery-ui-draggable', 'validate'), '1.0', true); and that works as long as I place it in the footer, not the header –  Richard Lane Aug 1 '11 at 11:41

I think you should just enqueue the script as well (for your validate script). I would wrap it around one whole function as well to avoid issue #11526: calling out

Thus all together:

<head>
...
<?php 

function mytheme_enqueue_script() {
// Load jQuery
wp_enqueue_script('jquery');
// Load draggable
wp_enqueue_script('jquery-ui-draggable');
//load your script
wp_enqueue_script('commonfunctions', child_template_directory . '/script/commonfunctions.js', array('jquery', 'jquery-ui-draggable'), '1.0', true);
// Load validate
wp_enqueue_script('validate','http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js', array('jquery'), true);
}

add_action('wp_enqueue_script', 'mytheme_enqueue_script');

Sorry, i haven't tested this yet. So i may be off.

share|improve this answer
    
jQuery should already be a dependancy for jquery-ui-draggable, so you can probably remove the enqueue for jQuery as it's likely unnecessary. Again, if commonfunctions has draggable as a dependacy, you don't need to enqueue it(that's the point of dependancies). –  t31os Jul 30 '11 at 9:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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