且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使type =“ module”的javascript进入队列。

更新时间:2023-12-05 08:17:16

一个人可以通过应用过滤器 script_loader_tag 来向脚本添加属性。

One can add attributes to a script by applying filter 'script_loader_tag'.

使用 add_filter('script_loader_tag','add_type_attribute',10,3); 添加过滤器。

定义回调函数,如上面链接中给出的示例:

Define the callback function like the example given on the link above:

function add_type_attribute($tag, $handle, $src) {
    // if not your script, do nothing and return original $tag
    if ( 'your-script-handle' !== $handle ) {
        return $tag;
    }
    // change the script tag by adding type="module" and return it.
    $tag = '<script type="module" src="' . esc_url( $src ) . '"></script>';
    return $tag;
}