Well – it works!
I still don’t really know what’s going on unfortunately, but the site is working with my script running with the Minit plugin in place – so thanks very much.
Could you indulge me to help understanding (I’m going to go and read up as much of the docs as I can but still…)?
What does “enqueue” actually means? I understand the english meaning (put in a queue), but not really following what WP is doing. I had imagined that there is an action hook “wp_enqueue_scripts” which runs during the creation of a page by WP. When that hook is reached, it loads any scripts which have been enqueued taking into account dependencies and whether to add them to the header or footer (depending on the params passed to wp_enqueue_script).
I’ve never seen any other mention of using wp_add_inline_script before when enqueueing is being discussed – the part I struggled to follow was how the script was actually called as opposed to loaded. wp_add_inline_script certainly addresses that as shown in the example you linked where it shows what ends up in the rendered page.
It seems odd that I’m having to give the path of the script file twice now – once to register and then again in the wp_add_inline_script.
For anyone coming to this thread later, here is the final code which worked in my case.
In child theme functions.php
/**** Enqueue script used for resetting dropdowns on contact form ****/
function adding_fc_scripts() {
wp_register_script('resetdropdowns', '/wp-content/themes/fc-zerif-lite-child/js/resetdropdowns.js', array('jquery'),'1.0', true);
wp_enqueue_script('resetdropdowns');
wp_add_inline_script ('resetdropdowns', 'resetdropdowns()');
}
add_action( 'wp_enqueue_scripts', 'adding_fc_scripts' );
In resetdropdowns.js
function resetdropdowns() {
code
});
}
Nothing added in the end to page template.
The rendered page source (just before the </body> tag amongst a bunch of other external script loads
<script type='text/javascript' src='https://dev.fairerppi.com/wp-content/themes/fc-zerif-lite-child/js/resetdropdowns.js?ver=1.0'></script>
<script type='text/javascript'>
resetdropdowns()
</script>
Note that the rendered output is slightly different to that shown in the previously linked example. It correctly prints two <script> blocks one for the src attribute and one for the actual script.