Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chavesm
Last active June 29, 2021 11:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chavesm/1c9eb33ad489ccaecc606401f9bbd1b3 to your computer and use it in GitHub Desktop.
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.
<?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 */
<?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 */
@chavesm
Copy link
Author

chavesm commented Apr 13, 2021

Async added.

Screenshot 2021-04-13 173430

Note that async added to inline scripts has no effect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment