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:
1 2 3 4 5 |
//Remove a function from the parent theme function remove_parent_filters(){ //Have to do it after theme setup, because child theme functions are loaded first remove_filter('init', 'function_to_be_removed'); } add_action( 'after_setup_theme', 'remove_parent_filters' ); |
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:
1 |
add_filter('init', 'function_to_be_removed'); |
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:
1 2 3 4 5 |
// Remove Admin bar function remove_admin_bar() { return false; } |
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:
1 |
add_filter('show_admin_bar', 'remove_admin_bar'); // Remove Admin bar |
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:
1 2 3 4 5 |
//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' ); |
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.
8 Comments on “How to Remove Filters Using Child Theme”
thank you for the post, it’s working.. 😀
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!
Glad you figured it out!
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
Thanks for this post! I had this exact problem and this helped me fix it!
” 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!
Thank you so much! This was driving me insane.
You’re welcome! I’m glad it helped. And I’m happy to see other Minneapolis-based web firms on here 🙂