Despite the name, it is used for enqueuing both scripts and styles. The wp_enqueue_script() function doesn’t support async/defer by itself, but luckily the script_loader_tag hook allows us to manipulate the wp_enqueue_script() output really easily. wpenqueuescripts is the proper hook to use when enqueuing scripts and styles that are meant to appear on the front end. If you need more info on why and when to use async/defer, you can read this article. Out of the box, WordPress does not include a async or defer attributes on javascript files loaded using wpenqueuescript (). Learn how to add async and defer attributes to WordPress enqueued script tags via functions.php. That’s okay, but some scripts you need them. They tell the browser to load and parse the JavaScript file separately, so the DOM can be shown immediately. The main benefits for using wpenqueuescript () are: You can set dependencies for the scripts you load, so for example you could add jquery as a dependency for your main. Defer Parsing of JavaScript: the Async and Defer Attributes To defer parsing Javascript in WordPress, you’ll need to mark the non-critical content with the Asyncand Defer attributes. If you’ve never heard about the wp_enqueue_script() before, please read this article before proceeding. Out of the box, WordPress does not include a async or defer attributes on javascript files loaded using wpenqueuescript(). wpenqueuescript () works as an additional layer for these wphead and wpfooter filters. To be able to understand this article you need to understand how wp_enqueue_script() function works.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |