Contents
At the moment, there is no way to add an in-line script to the WordPress footer and also load it with dependencies. But there is an alternative, if you’re willing. When WordPress works with scripts, it loads them internally to an object to keep track of them. There are essentially 3 lists inside the object:
Head, Footer and Post Injections interface To add code to your site’s header, you need to paste it into one of the boxes under the SECTION INJECTION area: Every page – adds the code snippet to the header of every single page on your site. This is what you’ll want to use most of the time.
How to put my JavaScript in the footer?
For an external javascript file to be linked in the footer, use this (>= WP2.8) That last true means that the script should be put at the wp_footer () hook. Thanks for contributing an answer to Stack Overflow!
How do I add code to my header in WordPress?
To add our code in the header or footer we will be using WordPress hooks. All code goes at the end of the child theme’s functions.php file. To add code to the header we will use the wp_head hook. You will need to paste something like this Adding code to the header. Similarly to add code to the footer we will be using the wp_footer hook.
For plugins we will be using plugins_url () function. The problem is that some times WordPress plugins add their own JavaScript to pages inside or inside page body. In order to move those scripts to the bottom you need to edit your plugin files and properly move the scripts to the bottom.
But this code snipped assumes that you’re the one queueing the script, i.e. you’re doing this in your own plugin or theme. At the moment, there is no way to add an in-line script to the WordPress footer and also load it with dependencies. But there is an alternative, if you’re willing.
Can you add JavaScript code to a WordPress header?
You may also come across WordPress tutorials that might ask you to add some custom CSS or Javascript code to your WordPress header or footer. By default, WordPress doesn’t provide an option to insert code in your website’s header and footer. Luckily there is an easy solution for beginners.