0

I'm creating a theme and i want to upload it to envato. I have a javascript file that is type module and i can enqueue it and then use script_loader_tag filter to add type module to it by these lines.

wp_enqueue_script( 'slider-js', get_stylesheet_directory_uri() . '/src/js/slider.js', array(), time(), true );

add_filter( 'script_loader_tag', 'add_type_attribute', 10, 3 );
function add_type_attribute( $tag, $handle, $src ) {

    if ( 'slider-js' !== $handle ) {
        return $tag;
    }
    $tag = '<script type="module" src="' . esc_url( $src ) . '"></script>';
    return $tag;
}

but the problem is when i run envato theme check and it gives this error:

REQUIRED: Found script_loader_tag in the file functions.php. Do not remove core functionality. Line 137: add_filter( 'script_loader_tag', 'add_type_attribute', 10, 3 );

Is there any other way to add type module ?

1
  • 1
    This seems like something you might need to take up with Envato
    – bosco
    Commented May 2, 2022 at 20:12

1 Answer 1

1

Lets try this:

  function slider_script () {
    wp_enqueue_script( 'slider-js', get_stylesheet_directory_uri() . '/src/js/slider.js', array(), time(), true );
  }
  function add_type_attribute( $tag, $handle, $src ) {
  
      if ( 'slider-js' !== $handle ) {
          return $tag;
      }
      $tag = preg_replace("/(.*)(><\/script>)/", '$1 type="module"$2', $tag);
      return $tag;
  }
  add_action('wp_enqueue_scripts', 'slider_script');
  add_filter( 'script_loader_tag', 'add_type_attribute', 10, 3 );
3
  • Please add an explanation for the code and what it does(especially explaining the regex), "lets try this" is not enough information. Commented Jan 23, 2023 at 12:33
  • Also the code just adds the type="module" to the script tag, it doesn't remove the original type="text/javascript" Commented Jan 23, 2023 at 12:57
  • Updated answer here: stackoverflow.com/a/75210085/4484799 Commented Jan 23, 2023 at 13:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.