10

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 Answers 6

13

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('');
7
  • 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
    Commented Jan 19, 2012 at 17:15
  • Glad to help :D Commented Jan 19, 2012 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
    Commented Jan 19, 2012 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 Commented Jan 19, 2012 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
    Commented Jan 19, 2012 at 17:59
2

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);
2

I know this is an old question, but there is a filter hook for this. You would add to your theme's functions.php or custom functionality plugin a function hooked to post_type_labels_{$post_type}

Take for example that we have a custom post type called band and we want to change the featured image labels to "Band Photo". The function would look something like this:

function wpse39446_modify_featured_image_labels( $labels ) {
  $labels->featured_image = __( 'Band Photo', 'textdomain' );
  $labels->set_featured_image = __( 'Set Band Photo', 'textdomain' );
  $labels->remove_featured_image = __( 'Remove Band Photo', 'textdomain' );
  $labels->use_featured_image = __( 'Use as Band Photo', 'textdomain' );

  return $labels;
}
add_filter( 'post_type_labels_band', 'wpse39446_modify_featured_image_labels', 10, 1 );

ref: https://developer.wordpress.org/reference/hooks/post_type_labels_post_type/

1

Since WordPress 4.4 the $screen arg can be an array which greatly simplifies mass additions or alterations of meta boxes.

The following code changes the title of the “Author” meta box to “Editor” on pages, posts, attachments, and all custom post types no matter how many are added or when they are added to your site.

add_action('do_meta_boxes', 'my_customize_meta_boxes'); //using do_meta_boxes also allows plugin metaboxes to be modified
function my_customize_meta_boxes(){
  $post_types = get_post_types();
  remove_meta_box( 'authordiv', $post_types, 'normal' );
  add_meta_box('authordiv', __('Editor'), 'post_author_meta_box', $post_types, 'side', 'default');
}
1
  • 1
    Worked great, thank you! I replaced $post_types with an array of the actual post types I wanted to affect, since get_post_types() returns all the known post types.
    – alexg
    Commented May 20, 2021 at 16:37
1

This is a bit hacky, but for anyone needing a simple CSS solution, use this:

.meta-box-sortables #your-metabox-id .ui-sortable-handle span {
    color: transparent;
}

.meta-box-sortables #your-metabox-id .ui-sortable-handle span:before {
    content: 'Your new title';
    display: inline-block;
    color: #000;
}

Just replace the your-metabox-id with your own. :)

(note: i normally add an admin.css through functions.php, that's where i control some wp admin styles)

0

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="%s">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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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