Today i was trying to find out how to disable Cloudflare’s Rocket Loader on certain scripts in WordPress, in normal HTML you would simply add the data-cfasync="false"
attribute tag but this isn’t possible in WordPress
However it turns out it’s also super simple if you know the correct filters and a bit of PHP.
Simply add the following code to your functions.php or code snippet plugin of your choice.
function cy_modify_wp_script_attributes( $attr ) {
// ID's of inline scripts that you want to disable Cloudflares Rocket Loader on
$rocketloader_disabled = array(
'core-framework-theme-loader-js-after',
'script2...',
'script3...',
);
if ( in_array( $attr['id'], $rocketloader_disabled, true ) ) {
$attr['data-cfasync'] = 'false';
}
return $attr;
}
add_filter( 'wp_inline_script_attributes', 'cy_modify_wp_script_attributes');
add_filter( 'wp_script_attributes', 'cy_modify_wp_script_attributes');
In the example above i am disabling Rocket Loader on the script with ID core-framework-theme-loader-js-after
but you can add any script you like, make sure to add any scripts it depends on also otherwise you will have issues due to order of scripts loading.