Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

like image 899
William H Avatar asked Jan 30 '13 14:01

William H


2 Answers

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');
like image 167
WillxD Avatar answered Sep 27 '22 21:09

WillxD


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

like image 32
McNab Avatar answered Sep 27 '22 22:09

McNab