Change The Title Of a Meta Box

I’m trying to create a function that would allow me to change the title of an established meta box (i.e, change Meta Box title ‘Authors’ to ‘Team’, etc.)

I didn’t want to use JS or have to unset the original meta box and re-add it.

I started off with the following as per another thread that listed the code like so:

// hook to the 'add_meta_boxes' action
add_action('add_meta_boxes', 'change_meta_box_titles');
function change_meta_box_titles($post_type, $post)) {
    global $wp_meta_boxes; // array of defined meta boxes
    // cycle through the array, change the titles you want
}

I’m stuck on the part to “cycle through the array and change the titles you want”.

What would be the best way to accomplish this? Using a foreach to loop? Or a Switch/Case scenario? I’m fairly new at this, could anyone provide an example of how to accomplish this?

Update: Stephen Harris’s example does work for Core Meta’s (thanks!):

add_action('add_meta_boxes', 'change_meta_box_titles');
function change_meta_box_titles() {
    global $wp_meta_boxes; // array of defined meta boxes
    // cycle through the array, change the titles you want

    $wp_meta_boxes['post']['normal']['core']['authordiv']['title']= 'Team Member';
}

Update: Fixed For Custom Meta’s

To get this to work with your custom meta’s change your add_action as follows so that it fires your change title code after the meta box has been added:

add_action('add_meta_boxes', 'change_meta_box_titles', 999);

6 s
6

Improved

I’ve decided to revisit this question after realising how unnecesarily hacky it is.

The the best solution is to to remove the metabox, and then re-add it, specifying an alternative title. Here’s an example for the post post-type.

add_action( 'add_meta_boxes_post',  'wpse39446_add_meta_boxes' );
function wpse39446_add_meta_boxes() {
    remove_meta_box( 'authordiv', 'post', 'core' );
    add_meta_box( 'authordiv', __('Team Member','wpse39446_domain'), 'post_author_meta_box', 'post', 'core', 'high' );
}

Note: If you are doing this for a non-core metabox, you’ll need to ensure the callback is called after the metabox is added by specifying a higher priority.


So, $wp_meta_boxes has a lot of nested arrays

For your purposes:

$wp_meta_boxes['post_type']['normal']['core']['authordiv']['title']= 'teams';

(NB. I’m not sure if any arguments are passed to that action…:

add_action('add_meta_boxes', 'change_meta_box_titles');
function change_meta_box_titles() {
    global $wp_meta_boxes; // array of defined meta boxes
    // cycle through the array, change the titles you want
}

Actually the array structure is more complicated. I’ve updated my code. I’ve tested this and it works 😀 (Just make sure you change 'post_type' to the post type, e.g. ‘post’).

Loosely the structure of the array is post-type > priority > core > metabox ID.

If you want to see the array for yourself, inside your function use:

echo '<pre>';
print_r($wp_meta_boxes);
echo '</pre>';
wp_die('');

Leave a Comment