wp_enqueue_script vs. wp_register_script

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?

Read More

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…

Related posts

Leave a Reply

2 comments

  1. 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.

  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”?