For a WordPress Theme I know javascript files (ending in .js) must be included using wp_enqueue_script
in functions.php
but how do I include additional javascript code? For example, say I need to add this:
jQuery(function($){
jQuery.supersized({
slides:
[
'http://google.com',
'http://google.co.uk'
]
});
});
Where would I put this? Do I put it within <script>
tags in the header.php?
Use
wp_localize_script
to pass data from php to your javascript:Just put it inside a callback, hooked into the correct action:
Since WP 4.5 the correct way to do this is to use
wp_add_inline_script
. This function preprends or appends your script to an enqueued script. For instance, you could append your script tojquery
like this: