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:
How do I add custom attributes to javascript tags in WordPress?
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 doesn’t ignore my JS files.
Here’s my code:
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 );
What is wrong with my code?
I’m currently using Rocket Loader on Automatic mode.
Can anyone help?
Maybe you can point me in the right direction.
Thank you.
I have found the solution for this!
As it’s written in this article:
Controlling Cloudflare Rocket Loader
Your script was almost right, but the manual mode is broken. You need to switch to automatic mode, and then make some modifications:
Notice in the example that the tag does not have the
type='text/javascript'
attribute. For some reason Rocket Loader requiresdata-cfasync='false'
to be used without thetype='text/javascript'
… a bug?Your code does add the
data-cfasync='false'
attribute, but does not override the WordPress behaviour of adding thetype='text/javascript'
attribute also, which makes Rocket Loader not to “ignore” your script.It might be tricky to override this WordPress behaviour since the relevant code does not support a filter…
Ernest Marcinko’s solution works fine; however a simpler (and potentially safer) solution these days is to use the
script_loader_tag
hook, added in WordPress 4.1.See this answer for an example.