Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@macbookandrew macbookandrew commented May 11, 2017

Changelog

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