How to use WordPress add_filter on child themes
Example 1
In the parent theme, in the functions.php you have:
$includes = array('a.php','b.php','c.php'); $includes = apply_filters( 'xyzabc_overridable_includes', $includes);
What happens is that you might be tempted, by analogy, to try the following in your child theme:
$includes = array('d.php', 'e.php', 'f.php'); $includes = add_filter('xyzabc_overridable_includes', $includes); //problem, this will not work!
The correct way to implement the hook in your child theme is:
function xyzdef_includes() { $includes = array('d.php', 'e.php', 'f.php'); return $includes; } add_filter('xyzabc_overridable_includes', 'xyzdef_includes' ); //this will work
Example 2
Let’s extend this furthermore and take for example another case:
a, b, c.php files are the originals in the parent theme
c.php file is our modified version placed in the child theme
and we want to include only c.php from the child theme, and let a.php and b.php load from the parent theme.
We can do this the following way:
function xyzdef_includes() { $includes = array( get_template_directory() . 'a.php', get_template_directory() . 'b.php', get_stylesheet_directory() . 'c.php'); return $includes; } add_filter('xyzabc_overridable_includes', 'xyzdef_includes' );