![]() 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) If (FALSE = strpos($url, '.js')) return $url 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. 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. ![]() Instead you’ll need to make use of the wp_enqueue_script function from within your theme’s functions.php file. ![]() Though it can probably be simplified somewhat. Enable opt out of the new behavior with a single filter (eg addfilter ( ‘wpoptimizescriptloading’, ‘returnfalse’ ) ) Audit existing front end enqueued scripts for defer strategy opportunities (eg. Use enqueue as normal, use script for CSS files with a false tag at the end so that it loads at the top. However, if you’re developing a WordPress theme, chances are you won’t simply be able to amend this HTML. Scripts added with wpaddinlinescript hooked on a deferred script should also be deferred. In a previous article I talked about adding the defer attribute to script tags to avoid blocking page render. ![]()
0 Comments
Leave a Reply. |