Add Javascript AND CSS To Specific WordPress Pages

Updated June 10th, 2020
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.

Lastly, replace PATH TO CSS FILE with the absolute or relative URL to the CSS file you want to include.

Advertisement
function load_css_js_assets() {
    if( is_page( ID ) ) {
        wp_enqueue_script('my-js', 'PATH TO JS FILE', array('jquery'), '', false);
        wp_enqueue_style('my-css', 'PATH TO CSS FILE');   
    } 
}

add_action('wp_enqueue_scripts', 'load_css_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:

Responses

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

WPLearningLab