The WordPress Codex doesn’t seem to have an answer for how to enqueue an inline script so that it can load with all the right dependencies — principally jQuery in most cases. Luckily, with some quick thinking, we can hook into the enqueue action and achieve this ourselves fairly easily.
Load Inline Script After jQuery WP Enqueue Script in WordPress
<?php function print_my_inline_script() { if ( wp_script_is( 'jquery', 'done' ) ) { ?> <script type="text/javascript"> // js code goes here </script> <?php } } add_action( 'wp_footer', 'print_my_inline_script' ); ?>
All we are doing is surrounding the inline script with a PHP function that hooks into the wp_footer action to fire only after jQuery has been loaded, along with the DOM.
Thank you for script. I think I will need it very soon. One more time thanx for share.
Izrada web stranica
Since it doesn’t enqueue the script with dependencies, is there a possibility that this will fire before jQuery has loaded? Would setting a later priority in add_action solve that?
Exactly.
Yes, this code appears to have a problem if you are looking for a script that is guaranteed to load your script after jQuery. Here, if the priority of this action is too high, the if statement will fail and WP execution will continue on (without ever loading your script). If you set the priority properly you would in theory never need the check in this code anyway(though it is a sanity check in case for some reason jQuery is changed).
You should give credit to Scribu, since your code is a one for one copy of this post on Stack Overflow.
http://wordpress.stackexchange.com/questions/24851/wp-enqueue-inline-script-due-to-dependancies
While it appears you are right, who cares who gets credit? This is about helping people solve problems, not some popularity contest. If it were on his blog, I’m sure he would have received credit.
I’m sure you’d care if someone was taking credit for the results of your education, experience, knowledge and skill/ It’s not about the creator wanting credit it’s about the creator of this post trying to take credit for themselves. If you copy someone elses code or other Intellectual Property the least you should do is give a subtle link back to the source.