You’re missing the function call for is_admin(). And you shouldn’t be checking that it’s false (viewing the front end)?
function myfuncton()
{
if( !is_admin() ){
wp_enqueue_script('myscript', $url_to . 'myscript.js', array('jquery','jquery-ui-core'));
}
}
add_filter('init', 'myfunction');
Thread Starter
Han
(@hswebdev)
Oops, I forgot the NOT in front of is_admin in my original post. The case still holds though. Basically, it should be saying:
“do not load the JavaScript in the admin panel, but load them on the front-end website”
I actually need it to say this:
“do not load the JavaScript in the admin panel, but load them in the front-end website (while logged in too)”
Hook it onto wp_enqueue_scripts rather than init and use add_action instead. Just tested in on a dev site and the scripts didn’t get included in the administrator section but showed up on the front end.
function myfunction()
{
if( !is_admin() ){
wp_enqueue_script('myscript', $url_to . 'myscript.js', array('jquery','jquery-ui-core'));
}
}
add_action('wp_enqueue_scripts','myfunction');
For future reference: admin_enqueue_scripts in the proper hook for including js in the admin section.
Thread Starter
Han
(@hswebdev)
I got it to work! Thank you Andrew!
I didn’t realize you had wp_enqueue_scripts. I thought you had admin_enqueue_scripts (wp vs. admin). There is a difference between the two according to the API. I also took out the is_admin part because we no longer need to check for that.
function myfunction()
{
wp_enqueue_script('myscript', $url_to . 'myscript.js', array('jquery','jquery-ui-core'));
}
add_action('wp_enqueue_scripts','myfunction');
Shouldn’t it be showing up when you’re logged in? All your logic check is doing is confirming that you’re not viewing the administrator area. If you don’t want it to be included for you but be included for everyone else can could add something like:
function myfunction()
{
global $current_user;
get_currentuserinfo();
if( !is_admin() && $current_user->user_ID != 1 ){
wp_enqueue_script('myscript', $url_to . 'myscript.js', array('jquery','jquery-ui-core'));
}
}
add_action('wp_enqueue_scripts','myfunction');
And replace 1 with your user id. If you don’t want it to show for logged in users, you can use is_user_logged_in().