I want to include and use third party JavaScript file (Bootstrap Tooltip) in my plugin. Since Bootstrap is highly popular framework and tooltip()
is rather generic name I want to make use of included noConflict()
functionality to isolate tooltip’s instance in my own variable.
If I understand timing at all right, this needs to be done right after script file loaded – so that nothing else has a chance to mess up instance (it had just set up) or get messed up by it either.
However there seems to be no convention in queue functionality to echo JS at such point. $wp_scripts->print_extra_script()
is called before the script.
Is this at all possible to accomplish with current queue implementation?
You have to filter
script_loader_src
, check if you are on the correct script and add a one-time filter forclean_url
. Then add your extra script, and make sure the output is syntactically valid.Pretty simple, albeit rather hacky.
Usage
This will add an inline script right after your enqueued script.
An alternative might be Patchwork. You can redefine WordPressâ code at runtime with that. But that wouldnât be less hacky.
WordPress >= 4.1
WordPress 4.1 had added
script_loader_tag
filter for complete HTML of enqueued script. Perfect for the purpose of adding something right after it.WordPress < 4.1
I ended up hooking into
wp_print_footer_scripts
at priority11
(after queue has finished) and callingwp_print_scripts()
explicitly on script’s handle, then echoing what I needed to.I would be hesitant to use this for something major, but for small contained script it seems acceptable.
Today I needed to do more or less same thing, ending up on an alternative solution, that I want to share in case it can be useful for someone.
I wrote a custom class extending
WP_Script
, something like this:This class inherit all methods from parent WP class, except one,
do_item
that is the one used to effectively print script on markup.GM_Scripts::do_item()
calls same method onWP_Script
and immediately after fires the custom action'after_script_done'
passing as arguments the script handle and a boolean that is true if the script was effectively print on page.After that, I hooked
wp_print_scripts
to overwrite the global$wp_scripts
object with an instance of my class, built using the static methodbuildFromWp
that inherits the status of aWP_Scripts
passed as argument.Once my instance inherits status, type and and methods of original WP class that should not cause any issue with WordPress or third parties code.
Also, this happen only on fronted, to further reduce any chance of conflicts.
Finally I added the action that echo what I need:
and enqueued my script as usual:
What’s the benefit of this approach against the accepted one?
First of all it is a more generic approach usable for every script, and more important, any dependency to my script is solved correctly.
As example, another piece of code can use
Using the accepted approach, in this case
if my script is already registered before
'wp_head'
it will be inserted in page without no conflict or whatever I want to print after itif it isn’t registereded the
'another-script'
will not be inserted because WordPress will not be able to solve the dependency