1

I am having problems enqueuing javascript. When I use the script tag in the head section of my site, everything works just fine. If I take the scripts out and use enqueue, the site breaks but I don't get any errors in firebug. Viewing the source code, the entire page looks as if it is there but obviously some of the js scripts are not working and this causes my site to crash. Is there a way to find out why it's crashing? The console has nothing in it.

I would link to the site but I am going to continue working and since things will change, the link won't be much good. I am looking for tips to debugging js errors. Where can I look?

my method of enqueueing the script

 wp_register_script('isotope', get_template_directory_uri() . '/js/jquery.isotope.min.js',array('jquery'),'',true);

 wp_enqueue_script('isotope');


 function bliss_jsscripts(){
   if( !is_admin()){
    wp_register_script('supersized', get_template_directory_uri() . '/js/supersized.3.2.6.js',array('jquery'), false, true);

    wp_enqueue_script('supersized','','',false,'');

    }
}
add_action('wp_enqueue_scripts', 'bliss_jsscripts', 999);
6
  • Could you add the script tag that you are using in the head section to enqueue the same script? Have you enabled WP_DEBUG, and do you receive any PHP/WordPress error messages? Have you tried registering the script to print in the header instead of the footer (by setting the last argument of wp_register_script() to false instead of true)? I'd also recommend setting the $version argument to false rather than an empty string '' while debugging this issue. Are you properly calling wp_enqueue_script() within a hook attached to the wp_enqueue_scripts action? In what file is this?
    – bosco
    Commented Feb 17, 2015 at 1:32
  • Thanks for the comments. I am working on your suggestions. I added to my question above to show more. I have tried setting the version to false in both the register and enqueue; however, the version is still there in the source code. I am calling everything from the functions.js file, wp_debug is set to true. I do not get any php errors. I have got some in firebug now and I think it might be related to the dreaded document.ready(function stuff. I have tried many different ways of wrapping these functions to get them to work
    – Jamie
    Commented Feb 17, 2015 at 2:00
  • why would I get a bunch of errors saying TypeError: $ is not a function when using enqueue but I don't get any of that when using a script tag. I just don't understand why putting a script in the header works just fine yet using enqueue to do the same thing causes type errors.
    – Jamie
    Commented Feb 17, 2015 at 3:05
  • Your code as it's written isn't attempting to do "the same thing" as a script tag in the header - when you're using wp_register_script() you're passing arguments to print your script in the footer - not the header - for starters. Additionally, when you call wp_enqueue_script() with all those empty strings, you're over-writing many of the arguments that you set in wp_register_script()... I recommend spending some time reviewing the Codex entries for these two functions before we troubleshoot this further.
    – bosco
    Commented Feb 23, 2015 at 18:31
  • For simplicity sake, let's just cut out the wp_register_script() entirely and use wp_enqueue_script() alone - it will register the script itself if it sees it's not already registered. Additionally, you shouldn't have any calls to wp_enqueue_script() outside of a function hooked to the 'wp_enqueue_scripts' action - the Codex mentions all of this, don't be afraid to dig ;) . It would also still be helpful if you could share the properly functioning script tag that you are attempting to reproduce with WordPress functions.
    – bosco
    Commented Feb 23, 2015 at 18:55

2 Answers 2

2

What you might do is check the status of the scripts you are trying to enqueue after doing so.

Also, be sure to enqueue things inside of an action.

e.g.

function do_my_enqueue_scripts() {
   wp_register_script('isotope', get_template_directory_uri() . '/js/jquery.isotope.min.js',array('jquery'),'',true);

   wp_enqueue_script('isotope');
}
add_action('wp_enqueue_scripts', 'do_my_enqueue_scripts');

You can use the wp_script_is function to check if they actually get enqueued.

e.g. after

wp_enqueue_script('isotope');
if ( ! wp_script_is('isotope', 'enqueued') ) {
    echo '<p>Script failed to queue up!</p>';
}
2
  • I will try the wp_script_is function to see if all of them are working. I do believe they are all enqueued. I counted.
    – Jamie
    Commented Feb 17, 2015 at 2:01
  • My issue seems to have resolved itself. I added some versions to the scripts rather than leaving it blank and that seems to have gotten them working.
    – Jamie
    Commented Mar 13, 2015 at 1:25
1

Your problem likely stems from using the WordPress functions incorrectly - there is probably no need to debug any Javascript to solve the issue. Carefully review the Codex entry for wp_enqueue_script().

I assume this is what you are attempting to accomplish. In your theme's functions.php file:

function wpse178370_enqueue_scripts() {
    wp_enqueue_script( 'isotope', get_template_directory_uri() . '/js/jquery.isotope.min.js', array('jquery') );
    wp_enqueue_script( 'supersized', get_template_directory_uri() . '/js/supersized.3.2.6.js', array('jquery'), '3.2.6' );
}

add_action( 'wp_enqueue_scripts', 'wpse178370_enqueue_scripts' );

Unless you're enquing a script at largely different points in your code, or swapping out script registrations on the fly, there's not too much of a reason to use wp_register_script() at all. Likewise, unless you explicitly need a script to be printed in the site footer (at wp_footer()) instead of within the <head></head> tags, you shouldn't be setting the $in_footer argument of wp_enqueue_script()/wp_register_script() to true.

If you register a script using wp_register_script() and specify all those optional arguments, then call wp_enqueue_script() with the same script tag and again specify all those optional arguments, you will override the arguments that you specified in wp_register_script().

Additionally, the is_admin() conditional check is uneccessary as dashboard scripts are printed on the 'admin_enqueue_scripts' action, not 'wp_enqueue_scripts'.

1
  • I am going to take things back to the beginning to when they were working with the script tags in the header. Then I will fix things up the way you suggested by only using the wp_enqueue_function and then I will edit my post above. Give me some time to make that change. Thank you for your comments. I have found them to be helpful.
    – Jamie
    Commented Feb 24, 2015 at 0:49

Your Answer

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

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