如果使用wp_enqueue_script添加脚本,如何绕过CloudFlare火箭脚本? [英] How to bypass CloudFlare rocket script if wp_enqueue_script is used to add scripts?

查看:94
本文介绍了如果使用wp_enqueue_script添加脚本,如何绕过CloudFlare火箭脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是用谷歌搜索,但是没有检索到任何特定信息。我在WordPress的PHP模板上有这样的代码:

I just googled this but didn`t retrieve any specific info. I have such code on a PHP template for WordPress:

<?php wp_enqueue_script( 'jquery-carousel', get_template_directory_uri().'/js/jquery.carouFredSel-6.2.1-packed.js', array('jquery'),'',true); ?>

我想为Rocketloader添加CloudFlare忽略 data-cfasync = false 在jquery.carouFredSel-6.2.1-packed.js的'src'属性之前

And I want to add CloudFlare ignore for Rocketloader data-cfasync="false" just before the 'src' attribute of jquery.carouFredSel-6.2.1-packed.js

我该怎么办?

问候

编辑:

非常感谢@Mary提供的代码。因此,解决方案是将该函数添加到functions.php中:

A big thanks to @Mary for the code. So the solution for this is to add this function in functions.php :

function add_data_attribute( $tag, $handle, $src ) {
    if ( 'jquery-carousel' !== $handle )
       return $tag;

    return str_replace( ' src', ' data-cfasync="false" src', $tag );
}

add_filter( 'script_loader_tag', 'add_data_attribute', 10, 3 );

如果需要添加更多标签,例如'jquery-carousel1','jquery-carousel2'此函数的代码如下所示:

If there is a need to add more tags like 'jquery-carousel1', 'jquery-carousel2' to this function, the code looks like this:

function add_data_attribute( $tag, $handle, $src ) {
    if( ! in_array( $handle, array( 'jquery-carousel', 'jquery-carousel1', 'jquery-carousel2' ) ) )
       return $tag;

    return str_replace( 'src', 'data-cfasync="false" src', $tag );
}

add_filter( 'script_loader_tag', 'add_data_attribute', 10, 3 );


推荐答案

您可以尝试使用 script_loader_tag

function add_data_attribute( $tag, $handle, $src ) {
    if ( 'jquery-carousel' !== $handle )
       return $tag;

    return str_replace( ' src', ' data-cfasync="false" src', $tag );
}

add_filter( 'script_loader_tag', 'add_data_attribute', 10, 3 );

这样,您可以定位特定的已排队脚本。

This way you can target your specific enqueued script.

这篇关于如果使用wp_enqueue_script添加脚本,如何绕过CloudFlare火箭脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆