我想重写子主题中的函数,该函数在父主题的类中定义。
这是示例代码:
class A extends B{
function __construct(){
$this->add_ajax('sync_post_data', 'need_to_override');
}
//other functions
function need_to_override(){
//function code
}
}
附加信息:
B 类扩展了 C 类,C 类是根类,其中add_ajax
被定义为。
我尝试过的:
- 由于该函数不可插入,因此我无法直接在子主题中覆盖函数。
-
其次,我尝试删除 ajax 操作并添加我的自定义操作。它抛出 500 内部服务器错误。
remove_action( 'wp_ajax_sync_post_data', 'need_to_override' );
add_action( 'wp_ajax_sync_post_data', 'custom_function' );
function custom_function(){
//function code with my custom modification
}
有什么帮助请...
您只需两个简单的步骤即可覆盖该类的方法。
就是这样:
- 打开子主题
functions.php
-
像这样创建新类:
add_action( 'after_setup_theme', function() {
class D extends A{
function need_to_override(){
//original function code with your custom modifications
}
}
new D();
});
PS:它会起作用,但我不确定这是否是最好的方法!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)