简体   繁体   中英

How to load JavaScript in WordPress

I need to know if this is the correct way to load JavaScript using functions.php

function wd_load_script() {
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', false, '1.10.2', true);
    wp_register_script('cufon-yui', get_template_directory_uri() . '/js/cufin-yui.js', 'jquery');
    wp_register_script('cufon-aller', get_template_directory_uri() . '/js/cufon-aller.js', 'jquery');
    wp_register_script('script', get_template_directory_uri() . '/js/script.js', 'jquery');
    wp_register_script('coin-slider.min', get_template_directory_uri() . '/js/coin-slider.min.js', 'jquery');

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'cufon-yui' );
    wp_enqueue_script( 'cufon-aller' );
    wp_enqueue_script( 'script' );
    wp_enqueue_script( 'coin-slider.min' );

}    
add_action('wp_enqueue_scripts', 'wd_load_script');

Yes that code is the correct way to load JavaScript in WP although the third parameter of wp_register_script should be an array. array( 'jquery' ) instead of 'jquery' .

For example change:

wp_register_script('cufon-yui', get_template_directory_uri() . '/js/cufin-yui.js', 'jquery');

To:

wp_register_script('cufon-yui', get_template_directory_uri() . '/js/cufin-yui.js', array( 'jquery' ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM