How to Remove Filters Using Child Theme

Disabling filters included in a parent theme using your child theme’s functions.php sounds like something that should be really easy, but it’s actually a little tricky to figure out. If you’re here, you probably already figured out that you need to use remove_filter, but might have noticed that it isn’t working.

The reason is probably that the child theme functions are being queued first, before any of the parent theme functions. What this means is that you are removing a filter before it has been added. Then, once it is added, there is nothing to remove it.

Here’s how we can properly remove a function:

What you need to fill out in remove_filter() is basically the same things that the parent theme used to define the filter in the first place. So in this case, the filter would have originally been added with the following code:

The first parameter is the hook that triggers this filter (basically just a timing event; for example, before WordPress is loaded, after theme is loaded, after plugins are loaded, etc.). In this case, I believe “init” is right after the theme is initialized. And the second parameter is the name of the function to be run at that time.

Let’s use a real-world example. For some reason, some themes completely remove the WordPress admin bar by default, using a filter. I don’t really have any idea why they do this, I find the bar quite helpful. One in particular is the HTML5 Blank theme, which is awesome, but disables the admin bar.

First, you’ll need to find that bit of code. Searching the parent theme’s functions.php for “Admin Bar” or “admin_bar” will probably find it for you. In HTML5 Blank, I found this section:

That’s the function that removes the admin bar. And where there’s a function, there’s a filter to activate it. Searching the rest of functions.php for the name of the function (in this case “remove_admin_bar”) should show you where the filter is. In this case, I found the following line:

Now we just need to use those parameters in a remove_filter function in our child theme’s functions.php, and it will be removed. Remember to make this a filter by itself as well, that gets triggered after the theme setup. Our code in this example will look like this:

And there you have it! The admin bar is removed. And of course, this should work with any other filter you need to remove from the parent theme.

About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

8 Comments on “How to Remove Filters Using Child Theme”

  1. It was working all along. I just forgot to change my theme to the child theme after creating it. Good grief! I feel like Charlie Brown!

  2. This does nothing.

    I’m using chlild theme confgurator.

    Here’s my funcions.php file:

    <?php
    // // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;

    // BEGIN ENQUEUE PARENT ACTION
    // AUTO GENERATED – Do not modify or remove comment markers above or below:

    if ( !function_exists( 'child_theme_configurator_css' ) ):
    function child_theme_configurator_css() {
    wp_enqueue_style( 'chld_thm_cfg_separate', trailingslashit( get_stylesheet_directory_uri() ) . 'ctc-style.css', array( 'normalize','html5blank','html5blank' ) );
    }
    endif;
    add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 20 );

    // END ENQUEUE PARENT ACTION

    I've tried adding

    //Remove the filter that removes the admin bar
    function remove_parent_filters(){ //Have to do it after theme setup, because child theme functions are loaded first
    remove_filter('show_admin_bar', 'remove_admin_bar');
    }
    add_action( 'after_setup_theme', 'remove_parent_filters' );

    both at the top and bottom of functions.php, but the admin bar does not show up regardless of where I add the code

  3. ” If you’re here, you probably already figured out that you need to use remove_filter, but might have noticed that it isn’t working.”

    Yepp! πŸ™‚

    Thanks!

Leave a Reply

Your email address will not be published.