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 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊