简体   繁体   中英

Add jQuery to WordPress page

I need to add jQuery library to WordPress page. The page is rendered by a plugin shortcode.

I've used the below methods but I don't see it working for my jQuery script.

I tried to add these below script in main plugin file where the plugin name and version go.

DESN'T WORK:

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

DESN'T WORK

function my_init() {
    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.4.2/jquery.min.js', false, '1.4.2'); 
        wp_enqueue_script('jquery');
    }
}
add_action('init', 'my_init');

I actually need to add below library to my WordPress front end page?

http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

but if i use it directly like below then it works.

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

Second, How can see it loaded in my browser loaded along with other code?

If you want to add script in wp-admin then use admin_head OR if you want to add this script on front-end then use wp_head in place of admin_head

Un-register all jQuery scripts and then call your function to add your custom script.

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );

function my_deregister_javascript() {
    wp_deregister_script( 'jquery' );
}

    function add_custom_script() {
        if (!is_admin()) {
              wp_enqueue_script('Jquery_host', "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" , array('jquery'));
         }
    }
    add_action('admin_head', 'add_custom_script');

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