How to load admin javascript files

Hi guys,

It’s my first time working with Sage + Bedrock :slight_smile:

I am trying to load an additional javascript file for the admin area only.

I have create a file themes/sage/resources/assets/scripts/test.js, which contains alert('test');

In themes/sage/app/admin.php I’ve added the admin_enqueue_scripts hook:

add_action('admin_enqueue_scripts', function () {
    wp_enqueue_script('sage/test.js', asset_path('scripts/test.js'), null, false, true);
});

The file seems to load in the admin area in the footer as expected. However, when I inspect the file contents, it’s empty and the alert call doesn’t execute.

I’m not getting any errors in the console.

Not sure what I’ve done wrong. Some help would be much appreciated.

Thanks,
Rob

I can’t spot anything obvious, except maybe the parameters at the end.

Not sure if this will help, but here’s an example of how I’m loading a js file into admin. Some minor changes shown.

  wp_enqueue_script('sage/customizer.js', asset_path('scripts/customizer.js'), ['customize-preview'], null, true);

Is the file definitely being compiled into your dist folder?

Well that was embarrassing :flushed: I forgot to npm run build.

Thanks for helping out @adamalexanderw

Ah! No problem, happens to all of us.

Glad it’s sorted.

This topic was automatically closed after 42 days. New replies are no longer allowed.