WordPress wp_enqueue_script Not Working

I’m developing a theme and trying to get wp_enqueue_script to work. The curious thing, is that nothing shows up. It doesn’t do anything. Here’s my setup:

in functions.php I have:

Read More
function named_scripts() {

    global $named_options;

    if( is_admin() ) return;

    wp_deregister_script( 'jquery' );
    wp_register_script( 'screen', tz_JS . '/screen.js', array( 'jquery' ) );
    wp_enqueue_script( 'screen' );
    wp_enqueue_script( 'bootstrap', tz_JS . '/bootstrap/bootstrap.js', array( 'jquery' ) );

    wp_register_style( 'custom-style', get_template_directory_uri() . '/css/custom-style.css', array(), '20120208', 'all' );  
    wp_enqueue_style( 'custom-style' );

}



add_action( 'init', 'named_scripts' );

in header.php I call

named_scripts();

And in the HTML, nothing shows up at all.

Related posts

Leave a Reply

4 comments

  1. You should have registered your jquery file after remove the default wordpress jquery. I use this code.. hope it helps..

    function load_external_jQuery() { // load external file  
        wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery  
        wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"), false);
        wp_enqueue_script('jquery');
        wp_register_script('blur', get_template_directory_uri() . '/_/js/blur.js', array('jquery') );
        wp_enqueue_script('blur');
    }  
    add_action('wp_enqueue_scripts', 'load_external_jQuery');
    
  2. Gerald is spot on. You’ve deregistered the jQuery that comes with WordPress without registering an alternative version.

    Normally the shipped version is removed if you want to load it directly from a CDN. An example would be below;

      wp_deregister_script('jquery');
      wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', false, '1.7.2');
      wp_enqueue_script('jquery');
    

    If you want to deregister it you need to register another version straight away before enqueing other JS dependant on jQuery

  3. Is the constant “tz_JS” is defined correctly? Presuming yes, you should be able to simplify your function like so:

    function named_scripts() {
    
        wp_enqueue_script( 'jquery' );
        wp_enqueue_script( 'screen', tz_JS . '/screen.js', array( 'jquery' ) );
        wp_enqueue_script( 'bootstrap', tz_JS . '/bootstrap/bootstrap.js', array( 'jquery' ) );
    
        wp_enqueue_style( 'custom-style', get_template_directory_uri() . '/css/custom-style.css', array(), '20120208', 'all' );  
    
    }
    add_action( 'wp_enqueue_scripts', 'named_scripts' );
    

    wp_enqueue_scripts is the proper hook to use for loading front-end scripts (see Codex). You don’t need to check is_admin() since admin_enqueue_scripts is the corresponding hook for loading scripts on the admin side.

  4. If you are developing a child theme use get_stylesheet_directory_uri() when loading js within your theme directory.

    function named_scripts() {
      wp_enqueue_script('jquery');
      wp_enqueue_script(
            'default_scripts', 
            get_stylesheet_directory_uri() . '/js/scripts.js', 
            array('jquery'),
            '1.0',
            false
            );
    }
    add_action( 'wp_enqueue_scripts', 'named_scripts' );