Page Speed Insights Remove unused JavaScript for Google Recaptcha

Viewed 5109

I have a site that scores well on Google Page Speed Insights, but it shows a performance issue that says "Remove unused JavaScript" for this file:

https://www.gstatic.com/recaptcha/releases/2diXFiiA9NsPIBTU15LG6xPf/recaptcha__en.js

However, I have tried removing my Invisible Captcha plugin, and also adding this line of code to functions.php:

add_action('wp_print_scripts', function () {
    if ( is_home() ){
        wp_dequeue_script( 'google-recaptcha' );
        wp_dequeue_script( 'google-invisible-recaptcha' );
    }
});

But I still get the error. Is there anything I can do to remove this script from loading on my home page? My site is using the latest version of Wordpress.

1 Answers

the below worked for me:

add_action( 'wp_enqueue_scripts', 'mytheme_deregister_scripts' );
function mytheme_deregister_scripts() {
  if( is_home() || is_front_page() ) :
    wp_deregister_script('google-invisible-recaptcha');
  endif;
}
Related