WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Disable plugin javascripts in admin, but enable on front-end only? (8 posts)

  1. Han
    Member
    Posted 1 year ago #

    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');
  2. Andrew Bartel
    Member
    Posted 1 year ago #

    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');
  3. Han
    Member
    Posted 1 year ago #

    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)"

  4. Andrew Bartel
    Member
    Posted 1 year ago #

    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');
  5. Andrew Bartel
    Member
    Posted 1 year ago #

    For future reference: admin_enqueue_scripts in the proper hook for including js in the admin section.

  6. Han
    Member
    Posted 1 year ago #

    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');
  7. Han
    Member
    Posted 1 year ago #

    This has been resolved.

  8. Andrew Bartel
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.