1

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
  • 1
    For those wondering how to do this: This might help you.
    – Enthusiast
    Commented Sep 11, 2016 at 9:25

1 Answer 1

2

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.

3
  • 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?
    – Enthusiast
    Commented Sep 10, 2016 at 7:40
  • You mean just before a library like script.js but after script1.js
    – bravokeyl
    Commented Sep 10, 2016 at 8:23
  • Yes, exactly! Is that possible?
    – Enthusiast
    Commented Sep 10, 2016 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.