Last active
June 29, 2021 11:42
-
-
Save chavesm/1c9eb33ad489ccaecc606401f9bbd1b3 to your computer and use it in GitHub Desktop.
Remove unnecessary "type" attribute from MI script code to keep HTML validators happy. Second file adds async attribute to external gtag script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Ignore this line if copying to child theme's functions.php | |
/** Remove the unnecessary "type" attribute from MI script code by leaving it out of the filter. */ | |
function remove_type_attr_1148073() { | |
return array( | |
'data-cfasync' => 'false' // Ask CloudFlare Rocket Loader not to execute. | |
); | |
} | |
add_filter( 'monsterinsights_tracking_analytics_script_attributes', 'remove_type_attr_1148073' ); | |
/** Add to child theme functions.php */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // Ignore this line if copying to child theme's functions.php | |
/** | |
* Remove the unnecessary "type" attribute from MI script CODE and add the "async" attribute | |
* to gtag.js script code. | |
*/ | |
function remove_type_attr_add_async() { | |
return array( | |
'data-cfasync' => 'false', // Ask CloudFlare Rocket Loader not to execute. | |
'async', // Adds the async attribute to the gtag library script tag. | |
// 'defer' SHOULD AVOID unless pagespeed is more important than accurate stats. If used, must remove async. | |
); | |
} | |
add_filter( 'monsterinsights_tracking_analytics_script_attributes', 'remove_type_attr_add_async' ); | |
/** Add to child theme functions.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Async added.
Note that async added to inline scripts has no effect.