Skip to content

Instantly share code, notes, and snippets.

@macbookandrew
Created May 11, 2017 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macbookandrew/30eb660833192066e5ea7691fc735e9f to your computer and use it in GitHub Desktop.
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
<?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 );
@macbookandrew
Copy link
Author

Changelog

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