简体   繁体   中英

Override metaboxes from parent theme in child theme in 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.

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 eg ( 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);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM