Please do not copy the file into your plugin/theme but use the WordPress library instead. When duplicating the hooks.js file, you have a huge problem, when another script loads the official hooks.js script on the same page. The second script will replace the wp.hooks logic of the first script (possibly deleting any JS hooks that were added already). Or even worse, you will get a JS error and the page does not initialize.
WP built an excellent logic to enqueue javascript that lets you define dependencies (i.e., tell WP which scripts need to be loaded, and in which order)
It's very easy to declare wp-hooks
a dependency of your script, via the third parameter of wp_enqueue_script()
.
For example:
<?php
wp_enqueue_script(
'your-script',
'url/to/js/your-script.js',
[ 'wp-hooks' ] // ← add this parameter.
);
Now, WordPress will automatically load 'wp-hooks' before your-script.js is loaded.
The second-best option is, to manually load the original wp-hooks
script without a dependency. You could write above code like that:
<?php
// Manually load the wp-hooks script.
wp_enqueue_script( 'wp-hooks' );
// Later enqueue your-script.js, with the guarantee that wp-hooks is also loaded
wp_enqueue_script( 'your-script', 'url/to/js/your-script.js' );
The benefits of this approach:
- No duplicate script problem!!
- It's the official WordPress way!
- It works on front-end and in wp-admin
- wp-hooks is only enqueued when actually needed, WP handles all the checks
- You are guaranteed to have the latest version of the script available
["hook"]
in the dependences when you enqueue your script.