Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress: Customize Page & Post Metaboxes
<?php
$wp_meta_boxes[ 'post' ][ 'normal' ][ 'core' ][ 'post excerpt' ][ 'title' ] = 'Abstract';
$wp_meta_boxes[ 'post' ][ 'normal' ][ 'core' ][ 'post excerpt' ][ 'id' ] = 'postabstract';
<?php
/**
* Display post excerpt form fields.
*
* @since 2.6.0
*
* @param object $post
*/
function post_excerpt_meta_box($post) {
?>
<label class="screen-reader-text" for="excerpt"><?php _e('Excerpt') ?></label><textarea rows="1" cols="40" name="excerpt" tabindex="6" id="excerpt"><?php echo $post->post_excerpt; // textarea_escaped ?></textarea>
<p><?php _e('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>'); ?></p>
<?php
}
<?php
function my_post_abstract_meta_box( $post ) {
?>
<label class="screen-reader-text" for="excerpt"><?php _e( 'Abstract' ) ?></label>
<textarea rows="1" cols="40" name="excerpt" tabindex="6" id="excerpt"><?php echo $post->post_excerpt; // textarea_escaped ?></textarea>
<p>
This is my custom text.
</p>
<?php
}
function customize_metaboxes( $post_type, $post ) {
global $wp_meta_boxes;
$wp_meta_boxes[ 'post' ][ 'normal' ][ 'core' ][ 'postexcerpt' ][ 'title' ] = 'Abstract';
$wp_meta_boxes[ 'post' ][ 'normal' ][ 'core' ][ 'postexcerpt' ][ 'id' ] = 'postabstract';
$wp_meta_boxes[ 'post' ][ 'normal' ][ 'core' ][ 'postexcerpt' ][ 'callback' ] = 'my_post_abstract_meta_box';
}
add_action( 'add_meta_boxes', 'customize_metaboxes', 10, 2 );
<?php
function customize_metaboxes( $post_type, $post ) {
global $wp_meta_boxes;
// inspect $wp_meta_boxes
echo "<pre>";
var_dump( $wp_meta_boxes );
echo "</pre>";
// customize ...
}
add_action( 'add_meta_boxes', 'customize_metaboxes', 10, 2 );
<?php
$wp_meta_boxes
[ posttype ] // dashboard, page, post, ...
[ column ] // normal, side
[ 'core' ]
[ box_handle ] // postexcerpt, trackbacksdiv, postcustom, commentstatusdiv,
// slugdiv, authordiv, submitdiv, formatdiv, categorydiv, tagsdiv-post_tag
[ box_attribute ] // id, title, callback, args
@mhulse

This comment has been minimized.

Copy link

commented May 9, 2013

Thnanks for this gist. For future readers, there's a related post here.

This is gist code is here as well (with more details).

Found in above blog post, this is useful to know:

These are the steps to customize the callback for metaboxes:

  • Open wp-admin/includes/meta-boxes.php.
  • Find the callback for the box you plan to adjust.
  • Copy it to your plugin or functions.php.
  • Prefix the function name to make it unique (myplugin_box_name).
  • Adjust it to your liking.
  • Alter the callback name in $wp_meta_boxes to the newly created function.

In my case, I wanted to remove the ping/trackback check box but keep the comments one:

/**
 * Customize meta boxes.
 *
 * @see http://core.svn.wordpress.org/trunk/wp-admin/includes/meta-boxes.php
 * @see http://wordpress.stackexchange.com/a/85740/32387
 * @see https://gist.github.com/eteubert/1327291
 */

function foo_add_meta_boxes($post_type, $post) {

    global $wp_meta_boxes, $current_screen;

    # Remove "ping_status" from `commentstatusdiv`:
    $wp_meta_boxes[$current_screen->id]['normal']['core']['commentstatusdiv']['callback'] = function($post) {

        ?>
            <input name="advanced_view" type="hidden" value="1">
            <p class="meta-options">
                <label for="comment_status" class="selectit">
                    <input id="comment_status" name="comment_status" type="checkbox" value="open" <?php checked($post->comment_status, 'open'); ?>> Allow comments?
                </label>
                <?php do_action('post_comment_status_meta_box-options', $post); ?>
            </p>
        <?php

    };

}

add_action('add_meta_boxes', 'foo_add_meta_boxes', 10, 2);

Thanks again!

Cheers,
M

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.