Created
May 11, 2017 20:52
-
-
Save macbookandrew/30eb660833192066e5ea7691fc735e9f to your computer and use it in GitHub Desktop.
Force the WPForms Google Recaptcha script to load asynchronously to improve site performance
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 | |
/* | |
* Plugin Name: WPForms: Force Async Scripts | |
* Plugin URL: https://gist.github.com/macbookandrew/30eb660833192066e5ea7691fc735e9f | |
* Description: Force the WPForms Google Recaptcha script to load asynchronously to improve site performance | |
* Version: 1.0.0 | |
* Author: AndrewRMinion Design | |
* Author URI: https://andrewrminion.com | |
*/ | |
if (!defined('ABSPATH')) { | |
exit; | |
} | |
/** | |
* Add async attribute if the path contains Google Recaptcha | |
* @param string $tag HTML <script> tag | |
* @param string $handle WordPress’ internal handle for this script | |
* @return string HTML <script> tag | |
*/ | |
function wpforms_recaptcha_add_async( $tag, $handle ) { | |
if ( strpos( $tag, 'recaptcha/api.js?onload=wpformsRecaptchaLoad' ) !== false ) { | |
$tag = str_replace( ' src', ' defer async="async" src', $tag ); | |
} | |
return $tag; | |
} | |
add_filter( 'script_loader_tag', 'wpforms_recaptcha_add_async', 99, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changelog