Support » Fixing WordPress » Disable plugin javascripts in admin, but enable on front-end only?

  • Resolved Han

    (@hswebdev)


    I use wp_enqueue_script() within my plugins to add JavaScripts files to the site. I don’t want to load these JS in the admin panel, but to load them when viewing the website (while logged in too). I tried using is_admin(), and it seems to work. However, the JS won’t load on the public website when I am logged in. Is there a function I can use to fix this? Thanks!

    function myfuncton()
    {
    	if( !is_admin ){
    		wp_enqueue_script('myscript', $url_to . 'myscript.js', array('jquery','jquery-ui-core'));
    	}
    }
    add_filter('init', 'myfunction');
Viewing 7 replies - 1 through 7 (of 7 total)
  • 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');

    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.

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

    This has been resolved.

    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().

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Disable plugin javascripts in admin, but enable on front-end only?’ is closed to new replies.