WordPress Parent Child ThemeOccasionally, I use a parent-child theme relationship to build sites. Many times, the parent themes come loaded with extras that get in the way, slow down the site, and just don’t need to be there. Here are few tips for cleaning up that extra bloat.

The following post provides code examples for adjusting parent theme functionality. We recommend creating a custom functionality plugin to place code rather than the functions.php theme file. If you are a novice, please be careful and always remember to backup. Note that we also provide a number of quality plugins as well. Disclaimer aside, lets get into the fun stuff.

De-enqueue Extra WordPress Parent Theme JavaScripts

One of the most common gripes I have with many themes is the number of JS files they output. Who wants smooth scroll anyways? I sure don’t. Let the browser handle that.  Take a look at the source of your parent theme. If you see extra scripts in there you don’t need, like a smooth scroll plugin, then remove them. In order to remove them, you need to do two things:

  1. Find the “handle” of the script or style
  2. De-enqueue the script or style with that handle

How do I find the handle of the script or style in my site?

There are a couple ways you can do this:

  1. With a plugin: Using the Debug Bar plugin and the add-on Debug Bar List Script & Style Dependencies we can output which scripts are loaded in each page of your site. The first line is the “handle” and the second line is exactly where you can find that script. The output is very nice:
    wordpress-debug-bar-script-style-deps
  2. Using an IDE: In PHPStorm I typically run a search for wp_enqueue_script through the parent theme directory. This will tell me all the scripts that are being output and I can unhook them from there.
  3. Using code: Here’s an excellent function from Ngoc Nguyen that will print out the script handles. You can insert this into a page template, create a page with that template, then visit that page and you’ll see the list output really nicely on the page. Just make sure you don’t use this on a live site:

How Do I De-Enqueue the Scripts?

Now that you have the handles of the styles or scripts that you want to de-enqueue, you’ve got to actually de-enqueue them. Here’s a few methods for doing that.

Using Code

I prefer to use code to keep the plugins to a minimum. Fortunately, de-enqueuing scripts and styles in WordPress is very easy. Add this to your custom functionality plugin:

For more information on wp_dequeue_script, please see the Codex article.

But sometimes you want to deregister a script or style only on certain pages. For example, WooCommerce likes to load scripts on every page of the site. This can cause load times to go up. Here’s an article where I explain, through the use of conditionals, how to load WooCommerce scripts conditionally. You can see in the comments people are experiencing significant reductions in load times.

Using a Plugin

Head Cleaner is plugin for removing scripts and CSS from your WordPress header and footer. Simply download the plugin, go to Settings > Head Cleaner, and select the scripts you don’t need output. If you’re not seeing and scripts or styles, try going to the frontend and refreshing a few times so the plugin can read the hooks.

head-cleaner-plugin


Deregistering an Unwanted Post Type

Let’s say you’ve purchased a Portfolio theme and it has a post type “testimonials”, but you don’t have any need for it this post type. Rather than having this post type sitting around on your site, let’s de-register it so we don’t have to see it every time we login to the admin panel.

Code Method: To deregister a post type programmatically here’s a nifty function provided by Nacin:

Then to use it simply run the function with your post type as the parameter:

Plugin Method: Adminimize is a good plugin for all aspects of customizing your WordPress admin panel, including removing post types. Please note: I believe the plugin will simply unhook the menu item from display rather than deregistering it though.

wordpress-adminimize-plugin

Removing Certain Post Type Features

Perhaps you don’t want to remove the entire “testimonials” post type, just a feature of the post type, such as excerpt or revisions support. remove_post_type_support() is a quick and easy function that will allow you to do just that:

For more about the remove_post_type_support function check out the Codex article.


Deregistering Sidebars You Don’t Need

Your child theme may not require certain registered sidebars from the theme’s parent. Let’s keep with our strategy of keeping our admin as clean and lean as possible and deregister unnecessary sidebars. The following code will prevent unwanted sidebars from appears under Appearances > Widgets in your WordPress admin:

The parameter expected for the unregister_sidebar is the ID of the sidebar when it was added.


Remove Custom Widgets from a WordPress Parent Theme

Some theme’s come with widgets baked in. A few may be useful, others not. So, let’s cut the fat and remove the ones we do not need. Typically, widgets are declared in a PHP class that extends the WP_Widget class. Find out the extended class name for the widgets you wish the remove by searching the parent theme for the aforementioned class name. Once you find them out use the following function to remove them:


What Else? Well, actually… a lot!

Themes have a lot of power to extend your WordPress site. Because of this, there may be other items that you wish to remove from a Parent theme such as:

  • Menus
  • Shortcodes
  • Image Sizes
  • Metaboxes
  • Actions & Filters

Here’s an excellent article from Tutsplus that provides more examples and functions for controlling parent theme behavior from within a child theme.

Thanks for stopping by! If you have any questions or comments then please leave one below.

Now in Video Format

The fine folks at Webucator put together a detailed Video tutorial based on this article. Enjoy!

Devin Walker is a San Diego-based WordPress Developer and enthusiast. He is the author of several popular and highly-rated WordPress themes and plugins. In his free time he enjoys playing Golf and traveling.

Follow Devin:

5 Responses to “How to Remove Styles, Scripts, and More from your WordPress Parent Theme”

  1. Ciaran Whelan

    Hey, thanks for sharing Devin, I just enjoyed the read of the Mailchimp article you sent out today. Your sharing of insights is fantastic. Bookmarked this page as I have a few themes in use that will find handy to deregister some crappy unwanted CPT’s that I will never use.

    Reply
  2. Peter Cralen

    Hi,

    Helpful post, anyway is it still actual to use wp_print_scripts? It looks like we have to use wp_enqueue_scripts instead.

    https://make.wordpress.org/core/2011/12/12/use-wp_enqueue_scripts-not-wp_print_styles-to-enqueue-scripts-and-styles-for-the-frontend/

    Reply
  3. Martin Sroka

    Hi,
    great post…it`s exactly that, i was looking for..also at woocommerce ^^ thank you

    cheers
    martin

    Reply
  4. Greer Pena

    Hello Admin,
    I have also created some similar kind of tools and scripts. he
    You can find them on my website https://goo.gl/9HpQFJ
    Please consider adding some of them in this post. towards

    Reply

Leave a Reply