By Sean Dezoysa


2019-05-14 10:26:41 8 Comments

I have read in another SO post that Underscore.js is including in Wordpress by default. I am using it to test if a variable is defined but get this in the browser console:

pluginAdminPage.js:32 Uncaught ReferenceError: _ is not defined
    at pluginAdminPage.js:32
(anonymous) @ pluginAdminPage.js:32

_.VERSION // .VERSION even autofills. So then the browser has the library imported?
"1.8.3"

The code it refers to:

if (_.isUndefined(module) === false) {
  module.exports.func = func;
}

Phpstorm lints _ and isUndefined yellow which may suggest a lack of library import.

I then tried to explicitly enqueue Underscore but this did not change the browser error messages:

function loadAdminScripts() {
  wp_register_style(
    'admin-style',
    plugins_url('admin-style.css', __FILE__)
  );
  wp_enqueue_style('admin-style');
  wp_enqueue_script('jquery');
  wp_enqueue_script('underscore');
}
add_action('admin_enqueue_scripts', 'loadAdminScripts');

What step(s) am I missing to get Underscore working?

2 comments

@kero 2019-05-14 10:51:17

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.

@Sean Dezoysa 2019-05-14 11:03:19

Thanks, I take it you are setting the scripts as dependencies of the stylesheet so they have priority? Is wp_enqueue_scripts() not that useful then?

@kero 2019-05-14 11:07:48

@SeanDezoysa Sorry, seems I read your question too quickly. You should set the scripts as a dependency of your script (pluginAdminPage), not the style.

@kero 2019-05-15 09:06:36

@SeanDezoysa Does this solve your problem now? The wp_register_script() part from your question is missing, so I had to assume how you're registering it

@Sean Dezoysa 2019-05-15 09:43:03

wp_enqueue_script() will also register the script file if argument 2 lists the path. I'll accept your answer now, thanks

@Sean Dezoysa 2019-05-15 02:37:19

Fixed by enqueuing underscore as a dependency of the js file:

function loadAdminScripts() {    
  wp_enqueue_script(
    'pluginAdminPage', 
    plugin_dir_url(__FILE__) . 'pluginAdminPage.js',
    ['jquery', 'underscore']
    );
  }
add_action('admin_enqueue_scripts', 'loadAdminScripts');

@leymannx 2019-05-15 06:45:48

Maybe better suggest an edit to the already given answer. Simply edit it and it will be queued for review.

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] How to use underscore.js in WordPress Admin

1 Answered Questions

Wordpress Jquery+scripts enqueue issue

2 Answered Questions

[SOLVED] underscore template dynamically remove row Jquery

1 Answered Questions

[SOLVED] Replace Underscore (_) on Space ( )

2 Answered Questions

[SOLVED] Remove H1 / title / Underscore - without CSS

3 Answered Questions

[SOLVED] Using wp_enqueue_script on shortcode function handler

1 Answered Questions

[SOLVED] Video script issue, JavaScript attribute remains 'undefined'

1 Answered Questions

Using wp_enqueue_script in a wordpress plugin

1 Answered Questions

[SOLVED] jQuery issue and wp_enqueue_script

1 Answered Questions

[SOLVED] Using Underscore Templates in WordPress

Sponsored Content