WordPress:在子主题中覆盖父主题类函数

我想覆盖子主题中的函数, 该子主题是在父主题的类内定义的。

这是示例代码:

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
}

任何帮助请…


#1


你只需两个简单的步骤即可覆盖该类的方法。

就是这样:

打开子主题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:可以, 但是我不确定这是否是最好的方法!

来源:

https://www.srcmini02.com/66248.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?