Admin enqueue scripts

Hello everybody,

I’ve seen no admin_enqueue_scripts hook in the theme.
Have you a special method for adding script only in admin panel?
Or maybe you just suggest me to add my own admin_enqueue_scripts hook in scripts.php

Thanks all!

Sounds like plugin territory. You won’t find anything like this in the theme.

This is a good opportunity to create a mu-plugin for your site specific functionality that’s independent from your theme.

1 Like

I’m facing the same problem at the moment and it’s not plugin territory for me.

My theme needs an admin page to make certain settings that affect the theme.

What I’ve tried so far, is to add my own admin.js file to the scripts folder and enqueue it properly in assets.php.

The problem I’m facing at the moment though, is that it doesn’t get built into the dist folder when running gulp. I’ve tried adding it to manifest.json but it’s just not copying it over.

How can I get this to work?

I had the same problem. My solution is to add this to assets.php ;

function admin_assets() {
    wp_enqueue_script('admin_scripts_js', asset_path('scripts/admin-scripts.js'), ['jquery'], null, true);
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\admin_assets', 100);

And then in manifest.json add:

"admin-scripts.js": {
      "vendor": ["assets/scripts/admin-scripts.js"]

Dont really know if it’s the best solution but it works. :smile: