wp enqueue script - Issue with using Underscore in Wordpress

I have read in another SO post that Underscore.js is including in Wordpress by default. I am using it to test if a varia

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?

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?

Share Improve this question asked May 14, 2019 at 10:26 Sean DSean D 3878 silver badges21 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

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.

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');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745492734a4630057.html

相关推荐

  • wp enqueue script - Issue with using Underscore in Wordpress

    I have read in another SO post that Underscore.js is including in Wordpress by default. I am using it to test if a varia

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信