admin-plugins author calendar category facebook post rss search twitter star star-half star-empty

Tidy Repo

The best & most reliable WordPress plugins

WP Enqueue Scripts: wp_enqueue_script() vs wp_enqueue_scripts for Loading WordPress JavaScript

WP Enqueue Scripts: wp_enqueue_script() vs wp_enqueue_scripts for Loading WordPress JavaScript

Ethan Martinez

October 11, 2026

Blog

Use wp_enqueue_scripts as the hook, and use wp_enqueue_script() as the tool that adds your JavaScript file. That is the whole trick. One is the doorbell. The other is the delivery person with the package.

TLDR

wp_enqueue_scripts is the WordPress action hook that tells WordPress, “Hey, now is the right time to load frontend scripts.” wp_enqueue_script() is the function you call inside that hook to load a real JavaScript file, like slider.js. Example: a small store moved one slider script from every page to only 12 product pages, and page requests dropped from 42 to 37 on the homepage. That cut load time by about 18%, which is not bad for five minutes of cleanup.

The tiny naming trap

WordPress gives us two names that look almost the same.

  • wp_enqueue_script() has no s at the end.
  • wp_enqueue_scripts has an s at the end.

Yes, that one small letter matters. A lot.

wp_enqueue_script() is a PHP function. You call it to register and load a JavaScript file.

wp_enqueue_scripts is an action hook. You attach your own function to it with add_action().

It drives me crazy that the names are this close. One typo can leave you staring at the browser console like it personally betrayed you.

Email Encoder for WordPress protected email

The simple mental model

Think of WordPress like a nightclub.

  • wp_enqueue_scripts is the time when the club opens the door.
  • wp_enqueue_script() is your friend walking in with a ticket.

If your friend arrives before the door opens, things get weird. If your friend never arrives, nothing loads. If your friend brings five extra friends who are not needed, the page gets slow.

That is why WordPress wants you to enqueue scripts the proper way.

The normal way to load JavaScript

Here is a basic example for a theme.

function mytheme_load_scripts() {
    wp_enqueue_script(
        'mytheme-main',
        get_template_directory_uri() . '/assets/js/main.js',
        array(),
        '1.0.0',
        true
    );
}
add_action( 'wp_enqueue_scripts', 'mytheme_load_scripts' );

Read it like this:

  • mytheme_load_scripts() is your custom function.
  • wp_enqueue_script() loads the file.
  • wp_enqueue_scripts tells WordPress when to run your function.

That last line is the glue. Without it, your function just sits there doing nothing. Like a plugin you installed “just in case” in 2019.

What the arguments mean

The wp_enqueue_script() function can look scary at first. It is not. It is just a list of facts about your JavaScript file.

wp_enqueue_script(
    $handle,
    $src,
    $deps,
    $ver,
    $args
);
  • $handle: A unique name for the script.
  • $src: The file URL.
  • $deps: Other scripts it needs first.
  • $ver: A version number for cache busting.
  • $args: Loading options, such as placing it in the footer.

The handle is not shown to users. It is for WordPress. Make it clear and unique. Use names like myplugin-gallery, not script. Future you will be less grumpy.

Loading a script that needs jQuery

Some scripts need jQuery. If so, list jquery as a dependency.

function mytheme_gallery_scripts() {
    wp_enqueue_script(
        'mytheme-gallery',
        get_template_directory_uri() . '/assets/js/gallery.js',
        array( 'jquery' ),
        '1.2.0',
        true
    );
}
add_action( 'wp_enqueue_scripts', 'mytheme_gallery_scripts' );

Now WordPress loads jQuery first. Then it loads your gallery script. No guessing. No duct tape. No sad console errors saying $ is not defined.

Why not just add script tags by hand?

You can paste a <script> tag into header.php. You can also eat soup with a fork. Both are possible. Neither is ideal.

Using wp_enqueue_script() gives WordPress control. That helps with:

  • Dependencies, so files load in the right order.
  • Duplicate prevention, so one file does not load twice.
  • Plugin compatibility, so other code can work with yours.
  • Performance, because you can load files only where needed.
  • Cache control, because version numbers help refresh assets.

Hand-coded script tags can cause tiny messes. Then those tiny messes become a three-hour hunt. Expect to waste time on the silliest thing, like one script loading two seconds too early.

Frontend vs admin scripts

wp_enqueue_scripts is for the public side of your site. That means pages, posts, archives, product pages, landing pages, and so on.

For the WordPress dashboard, use admin_enqueue_scripts instead.

function myplugin_admin_scripts() {
    wp_enqueue_script(
        'myplugin-admin',
        plugin_dir_url( __FILE__ ) . 'admin.js',
        array(),
        '1.0.0',
        true
    );
}
add_action( 'admin_enqueue_scripts', 'myplugin_admin_scripts' );

Do not load admin-only JavaScript on the frontend. Your visitors do not need it. Their browsers do not want it. Your speed score will glare at you.

Load JavaScript only where it is needed

This is where things get useful.

Say you have a contact form script. It should not load on every page. Load it only on the contact page.

function mytheme_contact_script() {
    if ( is_page( 'contact' ) ) {
        wp_enqueue_script(
            'mytheme-contact',
            get_template_directory_uri() . '/assets/js/contact.js',
            array(),
            '1.0.0',
            true
        );
    }
}
add_action( 'wp_enqueue_scripts', 'mytheme_contact_script' );

This keeps your site lean. A homepage does not need contact form validation. A blog post does not need checkout code. Simple rule: if the page does not use it, do not load it.

Modern loading options

Newer versions of WordPress let you pass more script options. For example, you can ask for defer.

wp_enqueue_script(
    'mytheme-main',
    get_template_directory_uri() . '/assets/js/main.js',
    array(),
    '1.0.0',
    array(
        'in_footer' => true,
        'strategy'  => 'defer'
    )
);

defer tells the browser to download the file without blocking the page. Then it runs after the HTML is ready. This can make pages feel quicker.

Use it with care. Some scripts need to run right away. Test after changing it. The browser console is your snack-sized lie detector.

Common mistakes

  • Using the wrong name: wp_enqueue_scripts() is not the function. The function is singular: wp_enqueue_script().
  • Forgetting add_action(): Your enqueue function must be hooked in.
  • Reusing handles: Each script needs a unique handle.
  • Loading everything everywhere: This slows pages for no good reason.
  • Skipping dependencies: If your script needs jQuery, say so.
  • Hardcoding URLs: Use theme or plugin URL helpers instead.

A quick cheat sheet

  • Need to load a JavaScript file? Use wp_enqueue_script().
  • Need to run that code on the frontend? Hook it to wp_enqueue_scripts.
  • Need the dashboard? Use admin_enqueue_scripts.
  • Need jQuery first? Add array( 'jquery' ).
  • Need better speed? Load scripts only on pages that use them.

The main thing to remember

wp_enqueue_scripts is the event. It is when WordPress says, “Frontend scripts can be added now.”

wp_enqueue_script() is the action you take. It is how you add one JavaScript file to the queue.

Use both together. Keep handles clear. Add dependencies. Load files only where needed. Your site gets cleaner, faster, and much less annoying to debug.