Skip to content

Instantly share code, notes, and snippets.

@madastro
Created December 13, 2016 06:49
Show Gist options
  • Save madastro/1ad90e67e4a11caac00ce0854040c4aa to your computer and use it in GitHub Desktop.
Save madastro/1ad90e67e4a11caac00ce0854040c4aa to your computer and use it in GitHub Desktop.
Sample code using WordPress script_loader_tag filter to defer scripts
<?php
// Do not load CF7 assets on other pages
if ( class_exists('WPCF7') ) {
add_filter( 'wpcf7_load_css', '__return_false' );
//add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_use_really_simple_captcha', '__return_true' );
// Add defer attribute
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
if ( 'contact-form-7' !== $handle )
return $tag;
return str_replace( ' src', ' defer="defer" src', $tag );
}, 10, 2 );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment