WordPress:覆盖子主题中的父主题类功能 [英] WordPress: Override parent theme class function in child theme
本文介绍了WordPress:覆盖子主题中的父主题类功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想覆盖子主题中的函数,它是在父主题的类中定义的.
I want to override function in child theme, which is defined inside a class in parent theme.
这是示例代码:
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
的根类.
Class B extends Class C and Class C is the root class where add_ajax
is defined.
我尝试过的:
- 由于该函数不可插入,所以我无法直接在子主题中覆盖函数.
其次,我尝试删除 ajax 操作并添加我的自定义操作.它引发 500 内部服务器错误.
- As the function is not pluggable so I can't override function directly in child theme.
Secondly I tried to remove ajax action and add my custom action. It throws 500 internal server error.
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
}
任何帮助请...
推荐答案
您只需两个简单的步骤即可覆盖该类的方法.
You can just override that method of class in just two simple steps.
方法如下:
- 打开子主题
functions.php
像这样创建新类:
- Open child theme
functions.php
Create new class like this:
add_action( 'after_setup_theme', function() {
class D extends A{
function need_to_override(){
//original function code with your custom modifications
}
}
new D();
});
PS:它会起作用,但我不确定这是否是最好的方法!
PS: It will work but I'm not sure if it is the best way or not!
这篇关于WordPress:覆盖子主题中的父主题类功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文