1
votes

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:

  1. As the function is not pluggable so I can't override function directly in child theme.
  2. 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

1 Answers

3
votes

You can just override that method of class in just two simple steps.

Here's how:

  1. Open child theme functions.php
  2. 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!