php - Loading custom javascript in a Wordpress plugin -


ok, driving me nuts:

i'm trying build simple wordpress plugin, , i'm trying make sure js separate php. i've gone through codex, , various tutorials, either making assumptions, or i'm idiot because it's not working...basically, i'm ultiamtely hoping add rows using ajax custom table when add post, first of i've got hello world working on add post page...

surely it's dead easy:

here's javascript in myplug/js/myplugin.js:

jquery(document).ready(function(){     alert("dothis"); }); 

here's version works in plugin (but bad):

function admin_load_js(){     echo '<script type="text/javascript" src="http://www.mysite.com/wp-content/plugins/myplugin/js/myplugin.js"></script>'; } add_action('admin_head', 'admin_load_js'); 

this marginally better, doesn't work (jquery not defined):

function admin_load_js(){     echo '<script type="text/javascript" src="http://www.mysite.com/wp-content/plugins/myplugin/js/myplugin.js"></script>'; } add_action('admin_enqueue_scripts', 'admin_load_js'); 

and way think should done, doesn't work @ doesn't 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 please give me clue here?? google isn't friend @ moment. maybe because it's late, don't know...

try 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 registers script. still need load script manually.

or use wp_enqueue_script @ same time. wp_enqueue_script, can specify dependencies (in case 'jquery') loaded before script.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -