Adding a jQuery script to wordpress Admin

≡放荡痞女 提交于 2019-11-30 06:33:19

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.

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

Change your admin_script.js to use jQuery instead $.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!