Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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);
share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

Improved Answer

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 :D (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('');
share|improve this answer
    
Stephen Harris I could kiss you. That worked like a charm, thanks so much! I was over complicating my code it seems when I was trying to sort this out on my own. –  Syrehn Jan 19 '12 at 17:15
    
Glad to help :D –  Stephen Harris Jan 19 '12 at 17:16
    
Hmm... I tried this with a custom meta box that I created "projectinfo" was the metabox unique id so then I tried... $wp_meta_boxes['post']['side']['core']['projectinfo']['title']= 'New Title'; but that didn't work, did I miss something here? –  Syrehn Jan 19 '12 at 17:45
    
Try echo '<pre>'; print_r($wp_meta_boxes); echo '</pre>'; wp_die(''); After you've changed the title to see whats gone wrong. My guess is that it is not 'core' :D –  Stephen Harris Jan 19 '12 at 17:55
    
I added that, the custom meta doesn't show up in the listing. So you're likely correct it's not a 'core'. –  Syrehn Jan 19 '12 at 17:59
show 1 more comment

Okay... This is a little hacky, but I kind of thought it was clever. Basically, you just use the built in language functions to change what you like. As long as you know the original word or words you want to change and they have been properly called out in the code with something like __('text in here'), you can change them to whatever you like.

I once used it to change the "Excerpts" meta box to a different name (along with the description inside) because my theme used it for extremely small pieces of text. Have a look:

/**
 * Here are some customizations that change text output via the gettext filter.
 * This was intended for translating themes to other languages, but why not
 * use it for more customization?
 *
 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
 *
 */
add_filter( 'gettext', 'change_excerpt_name', 20, 3 );
function change_excerpt_name( $translated_text, $text, $domain ) {

    if( $_GET['post_type'] == 'events' ) {

        switch ( $translated_text ) {

            case 'Excerpt' :

                $translated_text = 'Quick Summary';
                break;

            case 'Excerpts are optional hand-crafted summaries of your content that can be used in your theme. <a href="http://codex.wordpress.org/Excerpt" target="_blank">Learn more about manual excerpts.</a>' :

                $translated_text = 'Use this field to REALLY condense the description of this event.  Keep it around 12 words or less if possible. If you skip this field, the first few words in the area above will be used instead.';
                break;

        }

    }

    return $translated_text;
}

As it turns out, I wasn't the only one to think of this. Surprise. Here is a an article discussing the same idea, with a different method of using gettext.

share|improve this answer
add comment

Afaik, your best bet would be to hook a function to the hook just before meta box creation:

function alter_meta_box_titles( $post_type, $priority, $post )
{
    global $wp_meta_boxes;

    // Do check if you're on the right $post_type, $priority, etc.
    // Then alter the output
    foreach( $wp_meta_boxes as $index => $box )
        $wp_meta_boxes[ $index]['title'] = 'CUSTOM TITLE';

    return $wp_meta_boxes;
}
add_action( 'do_meta_boxes', 'alter_meta_box_titles', 0, 3);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.