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.

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

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

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

share|improve this answer
    
You should use the 'wp_enqueue_scripts' hook, not wp_footer. –  ferenyl Feb 1 '12 at 8:04
    
"You should use the 'wp_enqueue_scripts' hook, not wp_footer" - not if you're trying to remove it from the footer. If it's been enqueued in the footer, then you'll need to call remove_action() on wp_footer. –  Chip Bennett Feb 1 '12 at 13:15
    
Did some testing and you are correct. –  ferenyl Feb 1 '12 at 13:55
    
Thanks so much Chip! Question: in terms of tracking down / finding the offending call to jQuery in the footer. I normally use a boilerplate theme, but for a complex reasons, 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? –  Brian Feb 1 '12 at 16:54
1  
See updated answer re: Twenty Eleven script enqueueing. –  Chip Bennett Feb 1 '12 at 17:05
show 2 more comments

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');
share|improve this answer
    
Thanks so much! Question: in terms of tracking down / finding the offending call to jQuery in the footer. I normally use a boilerplate theme, but for a complex reasons, 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? –  Brian Feb 1 '12 at 16:54
    
look also in header.php, footer.php and also in plugins (if you have them) . BTW - twentyeleven is a really bad choice for customizing .. –  krembo99 Feb 2 '12 at 1:51
add comment

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.