简体   繁体   English

将jQuery添加到WordPress页面

[英]Add jQuery to WordPress page

I need to add jQuery library to WordPress page. 我需要将jQuery库添加到WordPress页面。 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. 我使用了以下方法,但我认为它不适用于jQuery脚本。

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? 我实际上需要将以下库添加到我的WordPress前端页面吗?

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 如果要在wp-admin添加脚本,请使用admin_head如果要在前端添加此脚本,请使用wp_head代替admin_head

Un-register all jQuery scripts and then call your function to add your custom script. 取消注册所有jQuery脚本,然后调用您的函数以添加您的自定义脚本。

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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