How do you install jQuery in Wordpress?

后端 未结 2 1456
-上瘾入骨i
-上瘾入骨i 2021-01-21 10:22

How can I install jQuery on my Wordpress site?

2条回答
  •  深忆病人
    2021-01-21 11:02

    Though one of the Answer is already Accepted but I feel the following technique may help somebody.

    Include the following code at Plugin Page OR at function.php

    function include_jQuery() {
        if (!is_admin()) {
            wp_enqueue_script('jquery');
        }
    }
    add_action('init', 'include_jQuery');
    

    Definitely you can use any meaningful function name instead off include_jQuery.

    Alternately, you can use the following code:

    function include_jQuery() {
        if (!is_admin()) {
            // comment out the next two lines to load the local copy of jQuery
            wp_deregister_script('jquery'); 
            wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js', false, '1.8.3'); 
            wp_enqueue_script('jquery');
        }
    }
    add_action('init', 'include_jQuery');
    

    Finally, as a general rule, you should not use the $ variable for jQuery unless you have used one of the shortcuts. The following is an example of how to shortcut jQuery to safely use the $ variable:

    jQuery(function ($) {
        /* You can safely use $ in this code block to reference jQuery */
    });
    

    You may like THIS link as well from where I personally learn the above technique(s). A Very BIG Thanks to Ericm Martin!

提交回复
热议问题