问题
I want to override function in child theme, which is defined inside a class in parent theme.
Here is the sample code:
class A extends B{
function __construct(){
$this->add_ajax('sync_post_data', 'need_to_override');
}
//other functions
function need_to_override(){
//function code
}
}
Additional Information:
Class B extends Class C and Class C is the root class where add_ajax
is defined.
What I've tried:
- 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 }
Any help please...
回答1:
You can just override that method of class in just two simple steps.
Here's how:
- 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: It will work but I'm not sure if it is the best way or not!
来源:https://stackoverflow.com/questions/53293310/wordpress-override-parent-theme-class-function-in-child-theme