Where should custom javascript be injected into a Wordpress theme?

I am helping a friend build a wordpress site using a purchased custom theme as his parent theme. There is also a child theme.

I've written some javascript to listen for clicks on specific links, and to change CSS in response to them. I can make this work in another environment, but we're trying to figure out how to get it to execute in Wordpress.

We have tried putting it in the index.php (most logical source to me, since that's to me that makes the most sense, but obviously WordPress works differently).

So all I am trying to figure out is, where in wordpress do you put the script tag (where you put code, not referencing jQuery we already have that)?


the index.php script will only load the WordPress framework and run it, you need to add your js code in the theme/template files.

a little quote from the official word press site:

JavaScript in Template Files

The safe and recommended method of adding JavaScript to a WordPress generated page, and WordPress Theme or Plugin, is by using wp_enqueue_script(). This function includes the script if it hasn't already been included, and safely handles dependencies.

you can read more here

In my opinion, the best is that you should create a js file with the code you want to run and upload to your theme/template folder, and then add it with a script tag as the link i've provided explains.

And my advice is to not embed the code directly in the template file but load it from a javascript file.

<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/pathto/yourscript.js"></script>

Need Your Help