Issue with using Underscore in WordPress

You need to set it as a dependency for your script, this is the best way you can make sure, Underscore.js is loaded before your script (which means, the methods will be available).

function loadAdminScripts() {
  wp_register_style(
    'admin-style',
    plugins_url('admin-style.css', __FILE__)
  );
  wp_register_script(
    'pluginAdminPage',
    plugins_url('pluginAdminPage.js', __FILE__),
    [
      'jquery',
      'underscore',
    ]
  );

  wp_enqueue_style('admin-style');
  wp_enqueue_script('pluginAdminPage');
}
add_action('admin_enqueue_scripts', 'loadAdminScripts');

Check the Code Reference on wp_register_style for more information.

Leave a Comment