Skip to content

How to Properly Modify JavaScript Files in a Parent Theme

I recently needed to modify a JavaScript file in a WordPress parent theme. The script was doing almost what I wanted, but not quite, so I wanted to make minor changes. In your case, you might want to entirely override a parent theme JavaScript file in your child theme, or even deactivate a parent theme JavaScript file altogether.

What Not to Do: Edit Parent Theme JavaScript Directly

The correct way to change a parent-theme JavaScript file is not to modify that file itself. The reason is simple: if your parent theme ever updates, you’ll lose all your changes. This is the whole point of WordPress child themes, and it’s as true for JavaScript as for anything else.

So all your changes will need to be in your child theme. If you’re making changes in your parent theme, you’re doing it wrong.

What to Do: Dequeue Parent Theme Script, then Enqueue Child Theme Script

You don’t edit or override parent theme JavaScript files. You stop the parent theme scripts from running, and replace them with similar scripts in the child theme.

Here’s the catch: WordPress doesn’t have a “template hierarchy” for JavaScript files, the way it does for PHP ones. In WordPress, there’s no such thing as “overriding JavaScript”—in a child theme, or anywhere else.

Yay! 🎉 You made it to the end of the article!
Fred Meyer
Share:

8 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Jai
September 15, 2020 9:15 am

Would this slow down a site?

Stender
March 5, 2019 8:31 am

Worked fine for me – hardest thing here, was to find out wtf the authors called the script as slug

David Hayes
March 6, 2019 5:13 pm
Reply to  Stender

Thank can definitely be hard 🙂

Sohaib Ahmad
July 12, 2020 1:17 pm
Reply to  Stender

How to find the handle to dequeue the script ?

Øyvind
August 17, 2017 10:26 am

did not work for me

Hassan
September 11, 2016 9:55 pm

I believe this fails (might also cause errors in page) if the script uses wp_localize_script(), since the localized strings would be missing.

fredclaymeyer
September 12, 2016 11:51 am
Reply to  Hassan

Thanks for the thought – agreed, you’d need to track down instances of wp_localize_script() that apply to the old script and re-point them to the new script with new wp_localize_script() calls in the child theme.

Nebulas Website Design
September 10, 2016 5:52 am

Thanks for the post very helpful.

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)!