This is a very clean way but there is one more script to download, meaning slower page loads. Create a snippet that dynamically injects the script, save it as a file and use the standard methods.When I strreplace ( ' src', ' defer src', tag ) the script, the returned value completely eliminates that first cache script, leaving only a commented out jquery script tag with ‘defer’ added. 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. Because of the above, there’s no way for me to replace ‘src’ with ‘defer src’. Scripts added with wpaddinlinescript hooked on a deferred script should also be deferred. The answers for this question detail the process, my problem is that it is very hacky (and probably could break in the future) and adding logic to clean_url has a performance cost - for one single script tag. In the script tag, the jquery script is commented out. Use the standard functions and use filters to modify the script tag. Out of the box, WordPress does not include a async or defer attributes on javascript files loaded using wpenqueuescript ().This is the easiest one and I have complete control over the script tag, but it is probably frowned upon (I intend to submit my plugin to the Plugin Repository so any advice regarding official guidelines is welcome). I found three possible solutions but I don't really like any of them: I am working on a plugin that has to inject a script on pages (something like RefTagger), which would be easy using wp_enqueue_script, but there are two reasons I cannot do that: the script tag has to have an ID and it should be executed asynchronously.
0 Comments
Leave a Reply. |