OK, this is driving me nuts:
I'm trying to build a simple Wordpress plugin, and I'm trying to make sure the js is separate from the php. I've gone through the codex, and various tutorials, but either they are all making assumptions, or I'm just an idiot because it's not working...Basically, I'm ultiamtely hoping to add some rows using ajax to a custom table when I add a post, but first of all I've just got to get this Hello World working on a Add Post page...
Surely It's dead easy:
Here's the javascript in myplug/js/myplugin.js:
jQuery(document).ready(function(){
alert("dothis");
});
Here's the version that works in the plugin (but is bad):
function admin_load_js(){
echo '<script type="text/javascript" src=".js"></script>';
}
add_action('admin_head', 'admin_load_js');
This is marginally better, but doesn't work (jQuery not defined):
function admin_load_js(){
echo '<script type="text/javascript" src=".js"></script>';
}
add_action('admin_enqueue_scripts', 'admin_load_js');
And this is the way I think it should be done, but doesn't work at all just doesn't do anything:
function admin_load_js(){
wp_register_script( 'custom_js', plugins_url( '/js/myplugin.js', __FILE__ ) );
}
add_action('admin_enqueue_scripts', 'admin_load_js');
Can someone please give me a clue here?? Even Google isn't my friend at the moment. Maybe because it's late, I don't know...
OK, this is driving me nuts:
I'm trying to build a simple Wordpress plugin, and I'm trying to make sure the js is separate from the php. I've gone through the codex, and various tutorials, but either they are all making assumptions, or I'm just an idiot because it's not working...Basically, I'm ultiamtely hoping to add some rows using ajax to a custom table when I add a post, but first of all I've just got to get this Hello World working on a Add Post page...
Surely It's dead easy:
Here's the javascript in myplug/js/myplugin.js:
jQuery(document).ready(function(){
alert("dothis");
});
Here's the version that works in the plugin (but is bad):
function admin_load_js(){
echo '<script type="text/javascript" src="http://www.mysite./wp-content/plugins/myplugin/js/myplugin.js"></script>';
}
add_action('admin_head', 'admin_load_js');
This is marginally better, but doesn't work (jQuery not defined):
function admin_load_js(){
echo '<script type="text/javascript" src="http://www.mysite./wp-content/plugins/myplugin/js/myplugin.js"></script>';
}
add_action('admin_enqueue_scripts', 'admin_load_js');
And this is the way I think it should be done, but doesn't work at all just doesn't do anything:
function admin_load_js(){
wp_register_script( 'custom_js', plugins_url( '/js/myplugin.js', __FILE__ ) );
}
add_action('admin_enqueue_scripts', 'admin_load_js');
Can someone please give me a clue here?? Even Google isn't my friend at the moment. Maybe because it's late, I don't know...
Share Improve this question asked Oct 6, 2013 at 20:32 Christian MayneChristian Mayne 1,7497 gold badges26 silver badges44 bronze badges1 Answer
Reset to default 8Try this:
function admin_load_js(){
wp_enqueue_script( 'custom_js', plugins_url( '/js/myplugin.js', __FILE__ ), array('jquery') );
}
add_action('admin_enqueue_scripts', 'admin_load_js');
wp_register_script just registers the script. You still need to load the script manually.
Or you could use wp_enqueue_script which does it all at the same time. With wp_enqueue_script, you can specify dependencies (in this case 'jquery') so that they will be loaded before your script.