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:
Why is this happening and how can I avoid it?
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);
}
Let me know if it works for you!
Please use the hook 'wp_enqueue_scripts' to enqueue css and js meant to be specifically used only on frontend. More details on https://developer.wordpress.org/reference/hooks/wp_enqueue_scripts/
function theme_enqueue_script() {
wp_enqueue_script('app-min', get_template_directory_uri() . '/app/app-min.js', false, 1.0, true);
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue_script' );