在WordPress管理员中添加jQuery脚本 [英] Adding a jQuery script to wordpress Admin

查看:122
本文介绍了在WordPress管理员中添加jQuery脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我无法让wordpress/wp-admin页面执行简单的查询文件. 仅当我在主题文件夹内的functions.php中取消注册jquery时,它才起作用,但随后我必须分别重新注册所有jquery.ui文件,这很繁琐.我正在使用wordpress 3.0多站点安装.我正在尝试不触及核心wp文件.

I cannot for some reason get the wordpress /wp-admin pages to execute a simple query file. It only works if i deregister jquery in my functions.php within my theme folder, but then i must re-register all the jquery.ui files separately which is tedious. Im using wordpress 3.0 multisite installation. I'm trying not to touch the core wp files.

它将显示在源代码中并链接到文件ok,但不会执行脚本. 这是我在functions.php中拥有的东西:

It will show in the source and links to the file ok but won't execute the script. heres what i have in my functions.php:

function my_script() {
if (!is_admin()) {
    wp_deregister_script('jquery');
    wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js', false, '1.4.4');
    wp_enqueue_script('jquery');
    wp_enqueue_script('custom_script', get_bloginfo('template_url').'/js/myScript.js', array('jquery'));
}
if(is_admin()){
    wp_enqueue_script('custom_admin_script',  get_bloginfo('template_url').'/js/admin_script.js', array('jquery'));
}   }

add_action('init','my_script');

add_action('init', 'my_script');

这是我的jquery文件(admin_script.js):

Here's My jquery file (admin_script.js):

$(document).ready(function(){
alert("Hello"); });

任何帮助都会很棒.

推荐答案

据我所知,Wordpress附带的jQuery在 NoConflict Mode 下运行,这意味着没有$ ,但改为jQuery.这可能就是您注销内置jQuery并使用Google CDN中的jQuery的原因.那可能不是在那种模式下运行的.

Be aware that the jQuery included with Wordpress runs in NoConflict Mode as far as I know, meaning that there is no $, but instead jQuery. That's probably why you deregistered the builtin jQuery and used the one from Google CDN. That one probably doesn't run in that mode.

我对Wordpress没有任何经验,所以在这里我可能会犯一个错误.只要确保内置的jQuery可用并加载您的脚本即可.

I don't have any experience with wordpress, so I might make a mistake here. Just be sure that the builtin jQuery is available and load your script.

function my_script() {
    if (!is_admin()) {
        wp_enqueue_script('custom_script', get_bloginfo('template_url').'/js/myScript.js', array('jquery'));
    }
    if(is_admin()){
        wp_enqueue_script('custom_admin_script', get_bloginfo('template_url').'/js/admin_script.js', array('jquery'));
    }   
}

将您的admin_script.js更改为使用jQuery代替$.

Change your admin_script.js to use jQuery instead $.

jQuery(document).ready(function(){
    alert("Hello"); 
});

看看是否适合您.如果您想使用$,则可以在admin_script.js的顶部写上var $ = jQuery;.

See if that works for you. If you like to use $ you could probably write var $ = jQuery; at the top of your admin_script.js.

这篇关于在WordPress管理员中添加jQuery脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆