javascriptphpwordpresswp-enqueue-scripts

WordPress how to enqueue custom script on frontend and avoid enqueueing it on the admin panel


I'm loading a custom script (app-min.js) into a WordPress site. From the site's functions.php:

wp_enqueue_script('app-min', get_template_directory_uri() . '/app/app-min.js', false, 1.0, true);

Works fine on the frontend, but the script is also loaded in the admin panel:

Screenshot, DevTools showing custom script being loaded in WordPress admin panel

Why is this happening and how can I avoid it?


Solution

  • You could exclude it from the admin panel by using is_admin condition. So your code would be something like this:

    if(!is_admin()){
      wp_enqueue_script('app-min', get_template_directory_uri() . '/app/app-min.js', false, 1.0, true);
    }
    

    is_adminDocs

    Let me know if it works for you!