dousao6313 2015-07-07 11:48
浏览 51
已采纳

从WordPress中的子主题中的父主题覆盖元框

I have a parent theme in WordPress that adds many functions and features into WordPress, including several metaboxes.

In my child theme, I have added some new custom post types into WordPress, so I am trying to modify the meta boxes to include them in the new custom post types.

Here is the parent theme functions.php file: http://pastebin.com/G1CVM6U2

My thought was that I would simply need to add code into my child theme that would remove the action from the parent theme that adds in the metaboxes, and then copy the metabox files into my child theme, modify them as needed, and then use add_action to pull in a new function that calls those metabox files.

However, I am having a hell of a time even being able to remove the action from the parent theme.

I have tried several variations of the following in my child theme functions.php:

remove_action('init', 'add_metaboxes');
remove_action('add_meta_box', 'add_metaboxes');

It does not remove the metaboxes though, as when I check the page/post editor, they are still visible.

Without running the remove_action, I cannot then do a new "add_action" to call in the modified metabox files. When I try to do so, I get a white screen of death due what I guess is the same function being redeclared.

I suspect that because the action in the parent theme is being called as part of a class, the process of over-riding this in the child theme might be differet?

Would appreciate some help on this one.

  • 写回答

1条回答 默认 最新

  • douvcpx6526 2015-07-07 12:23
    关注

    Sample 1: Remove a normal named add action

    add_action('after_setup_theme', 'remove_meta_boxes');
    
    function remove_meta_boxes(){
         remove_action('add_meta_box', 'add_metaboxes'), 10;
    }
    

    Sample 2: remove a function passed by reference

    the add_action looks like this:

    class ab{
    
        function __construct(){
            add_action('init', array( &$this, 'add_metaboxes' ));
        }
    
    
     }
    
     $x=new ab;
    

    The problem here is that wordpress will prepend a random identification string to the function name e.g. (a3898orj34930add_metaboxes), this is random every time so what we need to do is use strpos to find the name in the key. I have created a custom function for this.

    function remove_anon_filters( $name, $functionname, $priority){
        global $wp_filter;
    
        foreach ( $wp_filter[$name][$priority] as $key => $data) {
    
            if( stripos($key, $functionname )  ){
                remove_action( $name, $key, $priority  );
            }
    
         } 
    
     }
    

    We than then hook into after theme set up to remove the parent theme action.

     add_action('after_setup_theme', 'remove_parent_meta_boxes');
    
     function remove_parent_meta_boxes(){
        remove_anon_filters ( 'init', 'add_metaboxes', 10);
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: