Wordpress wp_enqueue_script Not Working

2019-03-29 19:11发布

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.

4条回答
beautiful°
2楼-- · 2019-03-29 19:53

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' );
查看更多
做个烂人
3楼-- · 2019-03-29 19:56

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

查看更多
不美不萌又怎样
4楼-- · 2019-03-29 19:58

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.

查看更多
祖国的老花朵
5楼-- · 2019-03-29 20:05

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');
查看更多
登录 后发表回答