Created
December 13, 2016 06:49
-
-
Save madastro/1ad90e67e4a11caac00ce0854040c4aa to your computer and use it in GitHub Desktop.
Sample code using WordPress script_loader_tag filter to defer scripts
This file contains 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 | |
// 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