Skip to content

How to Conditionally Enqueue a JavaScript File in WordPress

Enqueuing is the WordPress way to add a stylesheet or JavaScript file on a page. We already published a Quick Guide about including your JavaScript in WordPress. This time, though, we’re adding an extra wrinkle: how do you only enqueue a JavaScript file on certain pages of your WordPress site. That’s the focus of this here Quick Guide.

There are lots of reasons you may want to conditionally enqueue a file. Some examples:

  • You want a JavaScript pop-up to only appear on your homepage (or on all pages BUT your homepage)
  • You only want a JS file or CSS ruleset on a certain page
  • You only want to load a script or style for certain pages

All of these can be done with WordPress conditional tags. Here’s the video explaining how to do this:

How to Only Load a JavaScript File on Certain Pages in WordPress

  1. If you’ve not already made written the action hook, start there. You can follow this Quick Guide for the sake of doing this.
  2. We’re going to need to a if with a conditional tag. If you don’t know what conditional tag to use, check out the exhaustive Codex page of them. Some common and useful conditional tags:
    • is_single() tells you if you’re on a single-post or single-page view. Great of scripts that should only be on articles. Bonus: include a slug and it’ll only be shown on that specific one, like is_single('page-slug').
    • is_archive() tells your if the current page is displaying a set of archives, whether for an author, a tag, a date range, a category, etc.
    • is_home() is for when the front blog page is being display.
  3. Wrap your wp_enqueue_script or wp_enqueue_style call in an if () {} block. The conditional tag (or tags) you’re going to use goes in the parenthesis. The wp_enqueue_script goes in the curly braces ({}).
  4. You’re all set!

The power and simplicity of conditional tags is really great. I love how they read a lot like English! Hopefully this gets you a solid start.

Our Code for Conditionally Enqueuing a Script File

add_action('wp_enqueue_scripts', 'qg_enqueue');
function qg_enqueue() {
    if (is_home()) {
        wp_enqueue_script(
            'qgjs',
            plugin_dir_url(__FILE__).'quick-guide.js'
        );
    }
}

Further Reading on WPShout about Conditional Tags and Enqueuing Stylesheets or Scripts

https://wpshout.com/wordpresss-conditional-tags/

https://wpshout.com/everything-custom-scripts-styles-wordpress/

https://wpshout.com/quick-guides/use-wp_enqueue_script-include-javascript-wordpress-site/

David Hayes

3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Matt West
August 28, 2019 4:34 am

Why didn’t you add the example “is_page( ‘slug’ ) ” – not very in depth in my opinion.

Mark Tenney
November 6, 2017 8:40 pm

Would this work to implement intercom script for only admin and editor users in three ways (front end, admin, login)?

add_action('wp_enqueue_scripts', 'qg_enqueue');
function qg_enqueue() {
    if ( current_user_can('editor') || current_user_can('administrator') ) {
        wp_enqueue_script(
            'dgtl-intercom',
            plugin_dir_url(__FILE__).'js/dgtl-intercom.js'
        );
        login_enqueue_script(
            'dgtl-intercom',
            plugin_dir_url(__FILE__).'js/dgtl-intercom.js'
        );
        admin_enqueue_script(
            'dgtl-intercom',
            plugin_dir_url(__FILE__).'js/dgtl-intercom.js'
        );
    }
}
Fred Meyer
November 6, 2017 9:52 pm
Reply to  Mark Tenney

Hi Mark,

Without running the code itself, yes, that looks good at first glance! Definitely the current_user_can() checks look correct.

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!

Most Searched Articles

Best JavaScript Libraries and Frameworks: Try These 14 in 2024

In this post, we look at the best JavaScript libraries and frameworks to try out this year. Why? Well, with JavaScript being available in every web browser, this makes it the most accessible programming language of ...

25 Best Free WordPress Themes (Responsive, Mobile-Ready, Beautiful)

If you're looking for only the best free WordPress themes in the market for this year, then you're in the right place. We have more than enough such themes for you right ...

12 Best WordPress Hosting Providers of 2024 Compared and Tested

Looking for the best WordPress hosting that you can actually afford? We did the testing for you. Here are 10+ best hosts on the market ...

Handpicked Articles

How to Make a WordPress Website: Ultimate Guide for All Users – Beginners, Intermediate, Advanced

Many people wonder how to make a WordPress website. They’ve heard about WordPress, its incredible popularity, excellent features and designs, and now they want to join the pack and build a WordPress website of their own. So, where does one get ...

How to Start an Ecommerce Business: Ultimate Guide for 2024

Is this going to be the year you learn how to start an eCommerce business from scratch? You’re certainly in the right place! This guide will give you a roadmap to getting from 0 to a fully functional eCommerce business. ...