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.
I found three possible solutions but I don't really like any of them:
- Just echo the script in
wp_footerThis 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).
- Use the standard functions and use filters to modify the script tag. 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_urlhas a performance cost - for one single script tag.
- Create a snippet that dynamically injects the script, save it as a file and use the standard methods. This is a very clean way but there is one more script to download, meaning slower page loads.
Is there any other way to do this, or is any of my solutions acceptable?