admin管理员组

文章数量:1194410

I'm trying to add a script in the very last position before the closing via function.php

Other scripts are hooked by other plugins, so WP is placing my script as the firts one of the list. Like:

[my script] [the script generating the element I'd like to target]

Is there a way to tell Wordpress to hook my script as the last one before the closing tag, no matter what the other scripts are doing?

Thank you

I'm trying to add a script in the very last position before the closing via function.php

Other scripts are hooked by other plugins, so WP is placing my script as the firts one of the list. Like:

[my script] [the script generating the element I'd like to target]

Is there a way to tell Wordpress to hook my script as the last one before the closing tag, no matter what the other scripts are doing?

Thank you

Share Improve this question asked Jul 16, 2022 at 17:28 ScribbleScribble 1 0
Add a comment  | 

1 Answer 1

Reset to default 0

Assuming these are scripts enqueued via wp_enqueue_script(), the absolute best solution would be to locate the registration/enqueuement of the script creating the element and simply specify it's handle as a dependency for your script. WordPress will then ensure that yours loads after that dependency.

There are other work-arounds to ensure that your script is the absolute last, but they're not really elegant or pragmatic. Ultimately, if the script which creates the element does not do so immediately as it loads (and it's fairly unlikely that it does, in my experience), then your script must both come after the script in question as well as hook into the same or a latter JS event.

If you're unable to ascertain a script hook which you can depend on, you might consider using a MutationObserver or equivalent jQuery functionality in order to simply execute your JavaScript whenever the element becomes available.

Without seeing any of your code or the code on which depends, it's not possible to suggest which route you should take.

本文标签: javascriptHook a script as the very last one before the closing body tag