Search
Close this search box.

Add Javascript To Specific WordPress Pages

[updated_on]
Share this post:

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.

Share this post:

3 thoughts on “Add Javascript To Specific WordPress Pages”

  1. 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.

  2. Ronny Gunnarsson

    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)

Leave a Comment

Your email address will not be published. Required fields are marked *

WPLearningLab