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
    May 2 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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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