How do I “unhook” / de-register jQuery so that it’s not called as part of wp_footer();?

I need to “unhook” jQuery from my wp_footer() … it’s being called automatically and I’m looking for something I can add to functions.php which prevents it from being called a second time in the footer (since I need to call it in the header)

please no editorial comments about why calling in the footer is better

Related posts

Leave a Reply

2 comments

  1. You don’t want to unregister the script; that would make it unavailable to be enqueued. Rather, you want to dequeue the script.

    You have two choices:

    1. If you know the callback function through which wp_enqueue_script( 'jquery' ) is called, you can simply call remove_action( 'wp_footer', 'callback_function_name' )
    2. More likely, you won’t know the callback function (or jQuery will be enqueued as a dependency by another script), in which case, you can dequeue it directly, which you would do via wp_dequeue_script().

    e.g.

    function theme_slug_dequeue_footer_jquery() {
         wp_dequeue_script( 'jquery' );
    }
    add_action( 'wp_footer', 'theme_slug_dequeue_footer_jquery', 11 );
    

    Note: that priority is probably fairly important, since you’ll need to ensure that your dequeue call fires after the enqueue call.

    Edit

    I’m customizing this theme from the default TwentyEleven theme. It seems as though it’s the theme itself that’s hooking WordPress and not a plugin. I looked in the default functions.php and I don’t see any wp_enqueue_script() in there. Is there somewhere else I should look?

    Are you by chance viewing the Showcase Template (i.e. showcase.php)? Note that it enqueues a custom script:

    wp_enqueue_script( 'twentyeleven-showcase', get_template_directory_uri() . '/js/showcase.js', array( 'jquery' ), '2011-04-28' );
    

    It adds jQuery to its dependents array, meaning that WordPress will first enqueue jQuery, and then enqueue twentyeleven-showcase. So, that might be the source of the mystery jQuery enqueue.

    (There’s also a wp_enqueue_script() call in /inc/theme-options.php, but I don’t think that one would be impacting your front-end script-enqueueing.)

  2. the function to deregister any script is :

    wp_deregister_script('jquery');
    

    But the problem you have is different , if what I understand correctly – you have jQury included TWICE , once on header and once of footer . This is some plugin or theme function that is executed incorrectly – asking specifically to print in footer.. You might want to identify the problem better , for it can cause other problems …

    Anyhow, example hooks are : (one init , one print_scripts)

    function dl_register_common_js() {
    wp_deregister_script('jquery');
    }
    //add_action('init', 'dl_register_common_js'); // will deregister from head
    add_action('wp_print_scripts', 'dl_register_common_js');