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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With