Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php wp\u脚本\u添加\u数据不适用于异步延迟_Php_Wordpress_Asynchronous_Deferred - Fatal编程技术网

Php wp\u脚本\u添加\u数据不适用于异步延迟

Php wp\u脚本\u添加\u数据不适用于异步延迟,php,wordpress,asynchronous,deferred,Php,Wordpress,Asynchronous,Deferred,我目前正在努力使async和defer与wp\u脚本\u添加\u数据一起工作。为了完成这项任务,我偶然发现了以下文章。 但是,当我传递wp\u脚本\u add\u data时,它不会向脚本添加任何内容 下面是代码的样子: // Register/Enqueue the script source if it is configured if (array_key_exists('path', $s)) { // Replace constant tags with r

我目前正在努力使
async
defer
wp\u脚本\u添加\u数据一起工作。为了完成这项任务,我偶然发现了以下文章。
但是,当我传递
wp\u脚本\u add\u data
时,它不会向脚本添加任何内容

下面是代码的样子:

   // Register/Enqueue the script source if it is configured
    if (array_key_exists('path', $s)) {
      // Replace constant tags with real constants
      $s['path'] = self::localize($s['path'], $s['localize']);
      $src = self::scriptArgs($s['handle'], $s['path'], $s['in_footer']);

      wp_register_script(...$src);
      wp_enqueue_script(...$src);

      // // If set add async to script
      if ($s['async'] === true) {
        $t = $s['handle'];
        $t .= '-inline';
        // var_dump($src);
        wp_script_add_data('rollbar' , 'async/defer', true );
      }

      // Add inline script to source script if it exists
       if (array_key_exists('inline', $s)) {
         $s['inline']['contents'] = self::getFileContents($s['inline']['path'], $s['localize']);
         wp_add_inline_script($s['handle'], $s['inline']['contents'], $s['inline']['position']);
       }
    }
我正在将
async/defer
传递给
wp\u script\u add\u data
adter
wp\u enque\u script
,但是脚本没有添加
async defer
。我如何才能让这项工作呢

当我添加“条件”、“lt IE 9”时,它会起作用:

为了使wp\u脚本\u添加\u数据延迟一起工作,我定义了如下自定义过滤器。这很有效。:)

add_filter('script_loader_tag',函数($tag,$handle){
foreach(['async','defer']作为$attr){
如果(!wp_scripts()->获取_数据($handle,$attr)){
继续;
}
//防止在#12009中已添加属性时添加属性。
如果(!preg_match(“:\s$attr(|>|\s):”,$tag)){
$tag=preg_replace(':(?=>):',“$attr”,$tag,1);
}
//只允许异步或延迟,不允许两者都允许。
打破
}
返回$tag;
}, 10, 2);
添加\u操作('wp\u排队\u脚本',函数(){
wp_排队_脚本(
“稀有糖果资产”,
获取模板目录uri()。/dist/assets/main.js',
[],
filemtime(获取模板目录()。/dist/assets/main.js'),
真的
);
wp_脚本_添加_数据('rare-candy-assets','defer',true);
});