admin管理员组

文章数量:1122832

I'm trying to override a parent theme filter in a child theme; I must have the syntax wrong as the override is being completely ignored. Here's what's in my child theme's functions.php:

//* remove and replace parent filter
function child_remove_parent_function() {
    remove_filter('filter_name','parent_function');
}
add_filter('filter_name','child_function');

//* my custom function
function child_function($link){
    //* function code here
    return $link;
}

The child theme is definitely in use - I initially tried copying & customizing the function and got the 'cannot redeclare function' error. Just not sure why I can't replace the filter.

I even tried leaving just

function child_remove_parent_function() {
    remove_filter('filter_name','parent_function');
}

to see if the outputted link would break but that didn't do anything either. What am I missing?

I'm trying to override a parent theme filter in a child theme; I must have the syntax wrong as the override is being completely ignored. Here's what's in my child theme's functions.php:

//* remove and replace parent filter
function child_remove_parent_function() {
    remove_filter('filter_name','parent_function');
}
add_filter('filter_name','child_function');

//* my custom function
function child_function($link){
    //* function code here
    return $link;
}

The child theme is definitely in use - I initially tried copying & customizing the function and got the 'cannot redeclare function' error. Just not sure why I can't replace the filter.

I even tried leaving just

function child_remove_parent_function() {
    remove_filter('filter_name','parent_function');
}

to see if the outputted link would break but that didn't do anything either. What am I missing?

Share Improve this question edited Oct 4, 2015 at 14:40 Pieter Goosen 55.4k23 gold badges115 silver badges209 bronze badges asked Oct 4, 2015 at 14:39 Chez BallouChez Ballou 1231 silver badge11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I do not see you actually calling child_remove_parent_function() in your code.

Another issue to be aware of is timing. It is counter–intuitive, but functions.php files are loaded in order of child first, parent second.

Overall you need to ensure two things:

  1. code works at all
  2. it is called at the appropriate moment, after parent theme is done with its set up

I faced the same problem, and found the solution: to add a priority using the third parameter of the add_action function.

This is the definition of the function:

add_action( string $hook_name, callable $callback, int $priority = 10, int $accepted_args = 1 ): true

So, for example, instead of just: add_filter('filter_name','child_function'); ... this: add_filter('filter_name','child_function',20);

I hope it helps!

本文标签: functionsHow to override filter in child theme