I'm using wp_enqueue_scripts
to add jquery and other libraries. But now I wanna add a <script>alert('hello');</script>
before a certain library loads, how could i achieve that?
1 Answer
wp_enqueue_script
has $deps
parameter which exactly does what you need.
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer )
Let's say that your custom code is in a file named custom.js
which is enqueued with a handle my-custom
and you want this to be loded before my-script1
which is en-queued with a handle.
wp_enqueue_script( 'my-custom', '/path/to/customjs', array() );
wp_enqueue_script( 'my-script1', '/path/to/script1js', array('my-custom') );
The above code says that my-custom
is a dependency for my-script1
.
-
Thanks, I have done that, I created another file and enqueued it before this library and it works. But my question is can you add a
<script>alert('hello');</script>
tag before a certain library is loaded? Commented Sep 10, 2016 at 7:40 -
You mean just before a library like
script.js
but afterscript1.js
Commented Sep 10, 2016 at 8:23 -