问题描述
我正试图在我的WP网站上使用Cloudflare的Rocket Loader。除了WP Visual Editor以外,其他一切都正常。我遵循了这里的建议,但是没有用:
I'm trying to make Cloudflare's Rocket Loader work on my WP site. Everything works fine except for the WP Visual Editor. I followed the advice here but it doesn't work:
如何在Wordpress中向javascript标签添加自定义属性?
How do I add custom attributes to javascript tags in Wordpress?
Cloudflare说,为了使Rocket Loader忽略一个javascript文件,我需要在脚本之前添加data-cfasync = false标签:
Cloudflare says that in order to make Rocket Loader ignore a javascript file I need to add the data-cfasync="false" tag before my script:
<script data-cfasync="false" src="/javascript.js"></script>
Rocket loader不会忽略我的JS文件。
Rocket loader doesn't ignore my JS files.
这是我的代码:
function rocket_loader_attributes( $url )
{
$ignore = array (
'http://www.mysite.com/wp-includes/js/tinymce/tiny_mce.js?ver=349-21274',
'http://www.mysite.com/wp-admin/js/editor.js?ver=3.4.2'
);
if ( in_array( $url, $ignore ) )
{ // this will be ignored
return "$url' data-cfasync='false";
}
return $url;
}
add_filter( 'clean_url', 'rocket_loader_attributes', 11, 1 );
我的代码有什么问题?
我当前在自动模式下使用Rocket Loader。
I'm currently using Rocket Loader on Automatic mode.
有人可以帮忙吗?
也许您可以指出
谢谢。
推荐答案
I已经找到了解决方案!
I have found the solution for this!
如本文所写:
您的脚本几乎正确,但是手动模式已损坏。您需要切换到自动模式,然后进行一些修改:
Your script was almost right, but the manual mode is broken. You need to switch to automatic mode, and then make some modifications:
function rocket_loader_attributes_start() {
ob_start();
}
function rocket_loader_attributes_end() {
$script_out = ob_get_clean();
$script_out = str_replace(
"type='text/javascript' src='{rocket-ignore}",
'data-cfasync="false"'." src='",
$script_out);
print $script_out;
}
function rocket_loader_attributes_mark($url) {
// Set up which scripts/strings to ignore
$ignore = array (
'script1.js'
);
//matches only the script file name
preg_match('/(.*)\?/', $url, $_url);
if (isset($_url[1]) && substr($_url[1], -3)=='.js') {
foreach($ignore as $s) {
if (strpos($_url[1], $s)!==false)
return "{rocket-ignore}$url";
}
return "$url' data-cfasync='true";
}
return "$url";
}
if (!is_admin()) {
add_filter( 'clean_url', 'rocket_loader_attributes_mark', 11, 1);
add_action( 'wp_print_scripts', 'rocket_loader_attributes_start');
add_action( 'print_head_scripts', 'rocket_loader_attributes_end');
}
这篇关于Cloudflare的Rocket Loader + Wordpress->忽略脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!