

jQuery is also used by this plugin along with a vast amount of WordPress themes and other.
#Wp enqueue script defer code
To add Defer attribute add the following code to your theme’s functions. Since modifying the script tags inserted by wpenqueuescript is somewhat difficult, developers don't generally do this themselves, with plugins such as perfmatters attempting to do this automatically for the user. Deferring plugin scripts with the script defer attribute. The async attribute allows the browser to continue rendering the rest of your page while the API loads. The callback parameter executes the initMap function after the API is completely loaded. js/script.js, array ( jquery ), 1. The script loads the API from the specified URL. wpenqueuescript( handle, src, deps, ver, infooter) wpenqueuescript( script, gettemplatedirectoryuri(). The biggest issue with deferring scripts is that the defer attribute only works with external scripts.

Wp_enqueue_script('googleapis', esc_url( add_query_arg( 'key', $gioga_map_api.'&callback=initMap', '///maps/api/js' )), array(), null, true ) Since modifying the script tags inserted by wpenqueuescript is somewhat difficult, developers dont generally do this themselves, with plugins such as perfmatters attempting to do this automatically for the user. With this, inside your theme’s functions.php file: $gioga_map_api = YOUR_API_KEY To add the script in WordPress you need to replace this code: Why strreplace I’m using strreplace (the idea is from here) in the example because it’s the most efficient way to to do this. The handle is the name of your script and the first argument of the wpregisterscript or wpenqueuescript functions. The wpenqueuescript () function doesn’t support async/defer by itself, but luckily the scriptloadertag hook allows us to manipulate the wpenqueuescript () output really easily. Below are two functions that go into your functions.php file that add the defer attribute to your scripts.

Sometimes you need to add Defer and Async attributes to your scripts (e.g. The wpenqueuescript () function doesn’t support async/defer by itself, but luckily the scriptloadertag hook allows us to manipulate the wpenqueuescript () output really easily. If you need more info on why and when to use async/defer, you can read this article.
