将 jQuery 脚本添加到 wordpress Admin

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4927352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:11:24  来源:igfitidea点击:

Adding a jQuery script to wordpress Admin

jquerywordpress-themingcustom-post-type

提问by drav

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.

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

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:

它将显示在源代码和文件链接中,但不会执行脚本。这是我的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');

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

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

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

any help would be great.

任何帮助都会很棒。

回答by Reiner Gerecke

Be aware that the jQuery included with Wordpress runs in NoConflict Modeas 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 在NoConflict 模式下运行,这意味着没有$,而是jQuery. 这可能就是您取消注册内置 jQuery 并使用 Google CDN 的原因。那个可能不会在那种模式下运行。

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.

我对 wordpress 没有任何经验,所以我可能会在这里犯错。只需确保内置 jQuery 可用并加载您的脚本。

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

Change your admin_script.js to use jQueryinstead $.

更改admin_script.js使用jQuery代替$

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

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.

看看这是否适合你。如果你喜欢使用$你可能会写var $ = jQuery;在你的 admin_script.js 的顶部。

回答by NewUser

You can do like this

你可以这样做

<?php add_action( 'admin_enqueue_scripts', 'function_name' ); ?>

This can be used like this

这可以像这样使用

<?php add_action( 'admin_enqueue_scripts', 'load_custom_script' ); ?>
function load_custom_script() {
    wp_enqueue_script('custom_js_script', get_bloginfo('template_url').'/js/custom-script.js', array('jquery'));
}

For more help see the documentation here

如需更多帮助,请参阅此处的文档