3

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.

1

1 Answer 1

5

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;
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Can you add more details ? How do you cal add_id_to_script() function and what is data-app-key and what has to do with the question ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.