Using WordPress ‘post_excerpt_meta_box()’ PHP function

The post_excerpt_meta_box() WordPress PHP function displays the post excerpt form fields in the WordPress admin area.

Usage

post_excerpt_meta_box( $post );

Parameters

  • $post (WP_Post) (Required) – Current post object.

More information

See WordPress Developer Resources: post_excerpt_meta_box

Examples

Display the excerpt meta box for a custom post type

To display the excerpt meta box for a custom post type named ‘book’, you can use the add_meta_box() function.

function add_book_excerpt_meta_box() {
add_meta_box(
'book_excerpt',
'Book Excerpt',
'post_excerpt_meta_box',
'book',
'normal',
'high'
);
}
add_action( 'add_meta_boxes', 'add_book_excerpt_meta_box' );

Replace the default excerpt meta box

To replace the default excerpt meta box for a post, first remove the default one and then add the custom one.

function replace_default_excerpt_meta_box() {
remove_meta_box('postexcerpt', 'post', 'normal');
add_meta_box(
'postexcerpt',
'Custom Excerpt',
'post_excerpt_meta_box',
'post',
'normal',
'high'
);
}
add_action( 'admin_menu', 'replace_default_excerpt_meta_box' );

Add the excerpt meta box to a custom location

To add the excerpt meta box to a custom location on the edit screen, you can use the edit_form_after_title action.

function custom_excerpt_meta_box_location() {
global $post;
if ( 'post' === $post->post_type ) {
post_excerpt_meta_box( $post );
}
}
add_action( 'edit_form_after_title', 'custom_excerpt_meta_box_location' );

Display the excerpt meta box for multiple post types

To display the excerpt meta box for multiple post types, use a loop within the add_meta_boxes action.

function add_excerpt_meta_box_for_multiple_post_types() {
$post_types = array( 'post', 'page', 'book' );
foreach ( $post_types as $post_type ) {
add_meta_box(
$post_type . '_excerpt',
ucfirst( $post_type ) . ' Excerpt',
'post_excerpt_meta_box',
$post_type,
'normal',
'high'
);
}
}
add_action( 'add_meta_boxes', 'add_excerpt_meta_box_for_multiple_post_types' );

Customize the excerpt meta box label

To customize the label of the excerpt meta box, use the gettext filter.

function custom_excerpt_meta_box_label( $translated_text, $text, $domain ) {
if ( 'Excerpt' === $text ) {
$translated_text = 'Custom Excerpt';
}
return $translated_text;
}
add_filter( 'gettext', 'custom_excerpt_meta_box_label', 20, 3 );