The easiest way to include JavaScript functionality in your theme is to link directly to the file from the<head>
element of your theme. In this recipe, we'll examine how you determine the URL to link to and where to, put the linking element.
You need to have created a WordPress theme that contains at least a style.css
file and an index.php
file.
First, you're going to create a JavaScript file to link to. Open the folder that your theme lives in, and create a new folder called js
. This folder exists for the purposes of organization of your JavaScript files.
Inside of the js
folder, create a new file called my-theme.js
. Open this file for editing, and insert the following test script:
/* * Created for test purposes. */ alert('This is a test.');
Now you need to link to the JavaScript file from your theme, to ensure that the script is loaded and run. To do so, open up the theme file where your<head>
element...