简体   繁体   中英

How to add script as a module using wp_enqueue_script WordPress

I want to add a JavaScript module to a WordPress site. However by using wp_enqueue_script there is no option to set the type="module" in the <script> tag.

wp_enqueue_script( 'prodes-media-library-base', plugin_dir_url( __DIR__ ) . '/dist/js/classes/base.module.js', array(), '1.0' );

If I use the above to insert a script I cannot use import statement in my script.

If your script execution depends on attributes:

Ref: https://developer.wordpress.org/reference/hooks/script_loader_tag/

add_filter( 'script_loader_tag', 'add_id_to_script', 10, 3 );

function add_id_to_script( $tag, $handle, $src ) {
  if ( 'dropbox.js' === $handle ) {
    $tag = '<script type="text/javascript" src="' . esc_url( $src ) . '" id="dropboxjs" data-app-key="MY_APP_KEY"></script>';
  }

  return $tag;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM