![]() Your scripts’ src attribute will be replaced with itself along with defer and empty onload attributes resulting in them being loaded asynchronously and giving your site a dramatic performance boost. If (strpos($url, 'jquery.js')) return $url Īdd_filter('clean_url', 'defer_js', 11, 1) The key is to get the handle for each of the enqueued files and add it to the defer array. Since we can’t do this by using wpenqueuescript, we’ll use the scriptloadertagfilter hook. If (FALSE = strpos($url, '.js')) return $url Deferring a typical Javascript file is simply a matter of adding deferdefer to the Javascript file. ![]() While this isn’t a perfect solution it’s the most solid I’ve found so far and is what we currently implement here at Go Tripod: // Defer scripts Sounds pretty bad for performance, and it is, so we’ll be needing an extra something in our functions.php file. So we’ve got our scripts loading in the correct order – great! But unfortunately WordPress doesn’t load them asynchronously for you meaning your HTML will pause parsing while the script is downloaded and executed. Because plugins load assets via the wp_enqueue_scripts hook it’s important you do too so your theme plays nice with them. Your non-essential scripts will be deferred. In the plugin’s settings page, check the Individual JS files will be minified and deferred box. Autoptimize also offers the option to defer parsing your scripts. This function allows you to tell WordPress if your script has any dependancies, which you pass through as an array, and WordPress uses this information to load scripts in the correct order thus avoiding any JavaScript errors. Asset Clean Up defers combined JavaScript files by applying defer attribute to the script tags. Instead you’ll need to make use of the wp_enqueue_script function from within your theme’s functions.php file. However, if you’re developing a WordPress theme, chances are you won’t simply be able to amend this HTML. ![]() In a previous article I talked about adding the defer attribute to script tags to avoid blocking page render.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |