所以,我终于找到了一种方法来影响期望的函数。
我试图修改的函数包含在一个类中,所以我做了如下操作:
class my_Check extends Appointments {
function __construct() {
$this->unregister_parent_hook();
add_action( 'wp_ajax_post_confirmation', array( $this, 'post_confirmation' ) );
add_action( 'wp_ajax_nopriv_post_confirmation', array( $this, 'post_confirmation' ) );
}
function unregister_parent_hook() {
global $appointments; //this was the object created with the parent class
remove_action( 'wp_ajax_post_confirmation', array( $appointments, 'post_confirmation' ) );
remove_action( 'wp_ajax_nopriv_post_confirmation', array( $appointments, 'post_confirmation' ) );
}
function post_confirmation() {
...do the stuff with my mods...
}
}
$new_Check = new my_Check();只是,我现在有新问题了。父类在__construct()中做了更多的工作(许多add_action()'s,等等,$this填充了很多数据)。问题是,这些其他的东西和数据似乎没有带入子类。我尝试在孩子的__construct()函数中添加一个parent::__construct(),但似乎不起作用。
除了需要从父类继承的$this中有更多数据的情况外,我的mods代码可以正常工作。
如何在子类中维护父类的所有变量、函数、钩子和过滤器等?
而且,我不能用父类更改文件,因为它在插件核心文件中,我不想直接修改它。
发布于 2015-12-08 12:29:01
我见过很多这样的例子,人们使用parent::__construct()来传递父类的变量,等等,他们把它放在子类的__construct()函数中。这对我不起作用。
但是,通过在我添加到父类中的新函数中调用父构造函数,我终于能够让它工作。如下所示:
function post_confirmation() {
parent::__construct();
...do the stuff with my mods...
}从这篇文章中获得解决方案> https://stackoverflow.com/a/32232406/1848815
https://stackoverflow.com/questions/34068579
复制相似问题