Skip Navigation

[Resolved] I need to defer loading the recaptcha script for optimization purposes

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 7.57 hours from now. Thank you for your understanding.

This topic contains 1 reply, has 2 voices.

Last updated by Jamal 1 month, 1 week ago.

Assigned support staff: Jamal.

Author
Posts
#1822567

My Page Speed Insights are failing due to "unused javascript". The file in question is being loaded by gstatic.com. After searching the file structure of my wordpress site, it seems that it's coming from the Types plugin. I can't figure out how to disable this to improve my page speed. I'd be willing to use a different recaptcha plugin that's not built into Types that will give me more control over which pages it's loaded on as right now it's loaded globally, even on non "contact us" pages.

#1823251

Jamal
Supporter

Languages: English (English ) French (Fran├žais )

Timezone: Africa/Casablanca (GMT+01:00)

Hello and thank you for contacting the Toolset support.

Currently, there is no built-in way in WordPress or Toolset to achieve this. You will need to use custom code to change how the script is being added. For example, the following code will add the "defer" attribute to the script tag:

add_filter( 'clean_url', function( $url )
{
    if ( FALSE === strpos( $url, 'www.google.com/recaptcha/api.js' ) )
    { // not our file
        return $url;
    }
    // Must be a ', not "!
    return "$url' defer='defer";
}, 11, 1 );

You can explore different techniques in this StackOverflow thread https://stackoverflow.com/questions/18944027/how-do-i-defer-or-async-this-wordpress-javascript-snippet-to-load-lastly-for-fas

You may also want to use a 3rd Party plugin such as "Async JavaScript" https://wordpress.org/plugins/async-javascript/

Note that our script handler is called "wpt-cred-recaptcha". Check this screenshot hidden link

I hope this helps. Let me know if you have any questions.