0

I'll keep this short and sweet, I want to load a JS file in the admin panel as part of my plugin, and through tons of research I have a few different options that have all ended with the same problem that no one else seems to be experiencing so finally I'm posting for a solution here.

When I use the following code I get an error:

function register_scripts(){
    wp_register_script('payee',plugins_url('iBudget/modules/payee/payee.js',__FILE__));
}
function payee_init($hook){
    wp_enqueue_script('payee');
}

add_action ("admin_print_styles-post.php", "payee_init");
add_action ("admin_init", "register_scripts");

ERROR: /wp-content/plugins/iBudget/modules/payee/iBudget/modules/payee/payee.js/?ver=3.3.1:1 Uncaught SyntaxError: Unexpected token <

No matter how I attempt to enqueue the scrip I get this error even though the JS file simple says: alert("test");

Now the interesting nugget is this, if I include the JS directly in the function everything works fine:

function payee_init($hook){
?>
<script>
    alert("test");
</script>
<?
}
add_action ("admin_print_styles-post.php", "payee_init");

Basically I need to know why this error is coming up, I believe I'm loading the script correctly for what I need but not sure why this conflict is there and not even sure where else to look for a solution.

Thanks guys!

1 Answer 1

2

You action filter is wrong.

First change this function name function register_scripts , it's a bad name, use something unique.

Second change add_action ("admin_print_styles-post.php", "payee_init");

to a proper hook like add_action ("wp_enqueue_scripts", "payee_init");

http://codex.wordpress.org/Function_Reference/wp_enqueue_script

2
  • Thanks for your feedback on this. Had to take a quick break from that project so haven't had the chance to try your suggestion but I'll let you know once I do it. Thanks a lot! Commented May 9, 2012 at 22:40
  • wp_enqueue_scripts is for loading scripts on the front end. You'd want to use admin_enqueue_scripts for the admin side.
    – AlxVallejo
    Commented Oct 22, 2012 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.