Wordpress wp_enqueue_script not working

I am developing a theme and trying to get wp_enqueue_script to work. Curiously, nothing appears. He does not do anything. Here is 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 nothing appears at all in HTML.

+5
source share
4 answers

You should have registered your jquery file after uninstalling jQuery Wordpress by default. I am using this code .. hope this 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');
+8
source

Gerald spot. You unregistered the jQuery that comes with Wordpress without registering an alternate version.

Usually, the version you send is deleted if you want to download it directly from the CDN. An example will 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');

, , JS, jQuery

+4

Is the "tz_JS" constant defined correctly? Assuming yes, you should be able to simplify your function as follows:

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_scriptsis suitable for use in loading front-end scripts ( see Codex ). You do not need to check is_admin(), as admin_enqueue_scriptsthis is the appropriate hook for loading scripts on the administrator side.

+3
source

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

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' );
+1
source

All Articles