Paste the code below into your functions.php file. Then replace ID with the page ID of the page you want to include the files on. Check out the video tutorial if you need help finding the page ID.
Then, replace PATH TO JS FILE with the absolute or relative URL to the javascript or jQuery file you want to include.
function load_js_assets() {
if( is_page( ID ) ) {
wp_enqueue_script('my-js', 'PATH TO JS FILE', array('jquery'), '', false);
}
}
add_action('wp_enqueue_scripts', 'load_js_assets');
The false in the wp_enqueue_script function means that the script will be included in the header of the site. If you want the script added to the footer change the false to true.
3 thoughts on “Add Javascript To Specific WordPress Pages”
Hi! Can you help me? I have WP+woocommerce and I need to implement js-code into thankyou.php. I’ve seen your video, I’ve done everything, but don’t have the result.
This is exactly what I want to do. Run a javascript on one specific page. I did as instructed. To test I first only created one java file with the alert command you showed. The alert does not show up but I can see in the source that the java file is loaded. I am running WordPress. Is there any other setting I should change to enable the execution of the java file? (The alert should show up here: http://science-network.tv/signs-test-mcnemars-test/ and the java file is currently pairwise_test.js)
Hi Ronny,
I checked out that page using the Chrome Inspector and found that there is a Javascript error. Often if there are any JS errors on a page, other JS on the page won’t run. The error related to http://www.statcounter.com/counter/counter.js.
I think if you clear up that error the alert should work.
Let me know how it goes!