WordPress wp_enqueue_script() NEVER works

I’ve read all the posts and the documentation on wp_enqueue script… but I’ve NEVER successfully gotten it to work.

This is what I’m doing that works:

Read More
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"</script>
<script type="text/javascript">
MY CODE
</script>

I know I know I know this isn’t the right way to to do it. So here’s what I’ve tried instead:

wp_enqueue_script('jquery');
wp_head();

No luck.

add_action('init', 'addscripts');
function addscripts() {
    wp_enqueue_script('jquery');
 }
wp_head();

No luck.

//in the theme's functions.php file
add_action('init', 'addscripts');
//add_action('wp_enqueue_scripts', 'addscripts'); //this also doens't work
function addscripts() {
    wp_enqueue_script('jquery');
 }

I’ve also tried the same approaches with the link to the external jquery library at google instead of the jquery tag. I’ve also tried de-registering and then registering the jquery script. Never works.

So I think these are the key questions:

  1. Is there something wrong with my call to the wp_enqueue_script() function?

  2. Where should it go? functions.php? header.php?

  3. Is there a specific hook that I need to be using that’s not mentioned? 'init'? 'template_redirect'? 'wp_enqueue_scripts?'?

Please, no copy/pasted snippets from the codex! I’ve read it 50 times and tried them all. I promise.

I’m experimenting with a clean installation of WP3 and adding the above code to the default twentyeleven theme.

Related posts

Leave a Reply

3 comments

  1. We have to assume that wp_enqueue works because I am not seeing pitchforks and torches on the horizon.

    Such a thing would upset many developers.

    So given that, let’s assume that it’s never being called.

    Try adding something like this to the function that’s being called to unregister/register your new version of jQuery (not sure why you want to do this BTW).

    <?php error_log( 'Something called me!' , 0 ); ?>
    

    Then after you run your site and you expect your function to be called, go check your servers error log (/var/log/apache/yoursite-error.log or something) and see if that string shows up.

    If it is showing up, we can move on to the next set of troubleshooting.

  2. http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    Suppose you want to use the CDN copy of jQuery instead of WordPress’s,
    add this code to your functions.php file.

    <?php
    function my_scripts_method() {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js');
        wp_enqueue_script( 'jquery' );
    }    
    
    add_action('wp_enqueue_scripts', 'my_scripts_method');
    ?>
    
  3. I wanted to add a comment but presume the opening question is too old to do so.

    I hope I’ve quoted all the right people here and acknowledged their input.


    I had the same issue as @Emerson for a while until I did the following and how have jQuery working /loading.

    First I changed my JS code from $( to jQuery( as @Johannes Pille suggested in the opening bunch of comments. Information about the noConflict() which @Johannes Pille mentions can be found at: http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers

    That link to the codec is just for you @Emerson.. 😛

    So my code looks as follows – hope this helps someone.

    JS Code

    /**
     * navigation.js
     *
     * Handles toggling the navigation menu for small screens.
     */
    
    jQuery(document).ready(function(){
    jQuery(".nav-toggle").click(function(){
        jQuery("#menu-header-menu").slideToggle("fast");
    });
    });
    

    In my functions.php file

    function toggle_menu(){
    if(!is_admin()){
        //register the script location
        wp_register_script('navigation', 
            get_stylesheet_directory_uri() . '/js/navigation.js', 
            array('jquery'), '2.0.110526' );
        // enqueue the script
        wp_enqueue_script('navigation');
    };
    }
    add_action('init', 'toggle_menu'); 
    

    I found that telling WordPress the version of jQuery to use, ‘2.0.110526’ was very important. Without this it didn’t work.

    I didn’t have to add any of the OP’s examples of adding jQuery to the header.php or functions.php. As stated by many others WordPress comes shipped with jQuery. I’ve found that you just need to tell WordPress when and what version to use when registering your scripts in functions.php