How do I add a simple jQuery script to WordPress?

后端 未结 18 1869
心在旅途
心在旅途 2020-11-22 16:53

I read the Codex and a few blog posts about using jQuery in WordPress, and its very frustrating. I\'ve got as far as loading jQuery in functions.php file, but a

18条回答
  •  Happy的楠姐
    2020-11-22 17:37

    In WordPress, the correct way to include the scripts in your website is by using the following functions.

    wp_register_script( $handle, $src )
    wp_enqueue_script( $handle, $src )
    

    These functions are called inside the hook wp_enqueue_script.

    For more details and examples, you can check Adding JS files in Wordpress using wp_register_script & wp_enqueue_script

    Example:

    function webolute_theme_scripts() {
        wp_register_script( 'script-name', get_template_directory_uri() . '/js/example.js', array('jquery'), '1.0.0', true );
        wp_enqueue_script( 'script-name' );
    }
    add_action( 'wp_enqueue_scripts', 'webolute_theme_scripts' );
    

提交回复
热议问题