Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add additional attributes to script tag when enqueue script

I need Rocket loader to ignore some specific script that I enqueue using wp_enqueue_script(), So I need to add data-cfasync="false" to the script tag. But since I enqueue a js file via function.php, I have no tag to add this to it. Is there any way to do that?

like image 317
Avishay28 Avatar asked Oct 30 '25 08:10

Avishay28


1 Answers

I had a similar problem and this code added the attribute correctly.

Add this code snippet to your functions.php file to disable cloudfare for your specific script only. Just change YOUR_SCRIPT_HANDLE_HERE accordingly.

function disable_rocketship( $tag, $handle, $src ) {
    if ( 'YOUR_SCRIPT_HANDLE_HERE' === $handle ) {
        $tag = str_replace( 'src=', 'data-cfasync="false" src=', $tag );
    }
    return $tag;
}
add_filter( 'script_loader_tag', 'disable_rocketship', 10, 3 );
like image 91
vinnie Avatar answered Oct 31 '25 22:10

vinnie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!