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 badges2 Answers
Reset to default 1I 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:
- code works at all
- 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
版权声明:本文标题:functions - How to override filter in child theme? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312200a1935013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论