Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a jQuery script to wordpress Admin

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.

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

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

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

any help would be great.

like image 633
drav Avatar asked Feb 07 '11 22:02

drav


People also ask

Where do I put jQuery scripts?

Projects In JavaScript & JQuery It's always a good practice to add jQuery code in footer i.e. just before the closing </body> tag. If you have not done that, then use the defer attribute. The defer attribute is used to specify that the script execution occurs when the page loads.


2 Answers

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.

like image 65
Reiner Gerecke Avatar answered Oct 05 '22 18:10

Reiner Gerecke


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

like image 35
NewUser Avatar answered Oct 05 '22 17:10

NewUser