12

I found a WordPress hook called "edit_form_after_title" to add a text box after the title.

How can I use this hook to display the excerpt after the title while creating a new post?

6 Answers 6

7

It's simple, just unregister postexcerpt box first then add another one on the top.

Here is my code

add_action(
  'admin_menu', function () {
    remove_meta_box('postexcerpt', 'post', 'normal');
  }, 999
);

add_action('edit_form_after_title', 'post_excerpt_meta_box');
2
  • 1
    Hey +1 on that, but how do you address the styling after the removal of the meta_box?
    – user9447
    Sep 22, 2014 at 2:24
  • Doesn't work anymore after block editor is introduced
    – Fanky
    Jun 9, 2022 at 12:22
6

I adapted from here: https://wordpress.stackexchange.com/a/158485/373

/* -----------------------------------------
 * Put excerpt meta-box before editor
 * ----------------------------------------- */
function my_add_excerpt_meta_box( $post_type ) {
    if ( in_array( $post_type, array( 'post', 'page' ) ) ) {
         add_meta_box(
            'postexcerpt', __( 'Excerpt' ), 'post_excerpt_meta_box', $post_type, 'test', // change to something other then normal, advanced or side
            'high'
        );
    }
}
add_action( 'add_meta_boxes', 'my_add_excerpt_meta_box' );

function my_run_excerpt_meta_box() {
    # Get the globals:
    global $post, $wp_meta_boxes;

    # Output the "advanced" meta boxes:
    do_meta_boxes( get_current_screen(), 'test', $post );

}

add_action( 'edit_form_after_title', 'my_run_excerpt_meta_box' );

function my_remove_normal_excerpt() { /*this added on my own*/
    remove_meta_box( 'postexcerpt' , 'post' , 'normal' ); 
}
add_action( 'admin_menu' , 'my_remove_normal_excerpt' );
0
2
function jb_post_excerpt_meta_box($post) {
    remove_meta_box( 'postexcerpt' , $post->post_type , 'normal' );  ?>
    <div class="postbox" style="margin-bottom: 0;">
        <h3 class="hndle"><span>Excerpt</span></h3>
        <div class="inside">
             <label class="screen-reader-text" for="excerpt"><?php _e('Excerpt') ?></label>
             <textarea rows="1" cols="40" name="excerpt" id="excerpt">
                  <?php echo $post->post_excerpt; ?>
             </textarea>
        </div>
    </div>
<?php }

add_action('edit_form_after_title', 'my_post_excerpt_meta_box');

In this way, you are able to add exactly an excerpt box as you like. But it is important to eliminate the original box. If not, you will not be able to save the excerpt in the new box.

2

This answer is similar to the one @OzzyCzech posted, but it's more universal and it adds a header to the excerpt box. One downside to this method is that you can't hide the excerpt box via Screen Options... in that case you'd need to use the answer by @lea-cohen.

add_action( 'edit_form_after_title', 'move_excerpt_meta_box' );
function move_excerpt_meta_box( $post ) {
    if ( post_type_supports( $post->post_type, 'excerpt' ) ) {
        remove_meta_box( 'postexcerpt', $post->post_type, 'normal' ); ?>
        <h2 style="padding: 20px 0 0;">Excerpt</h2>
        <?php post_excerpt_meta_box( $post );
    }
}
0

If you want to move the postexcerpt for a WooCommerce PRODUCT (short description), you have to use a different hook to get rid of the original postexcerpt. (If you don't, you can't really edit it from your new box at the top because it still exists at the bottom.)

I spent hours on this to remove the WooCommerce short description metabox (and then add it up above the main content editor). I could ONLY get rid of the postexcerpt by using the add_meta_boxes hook. admin_menu and adminhead ran too early. So I removed everything else at the same time.

The rest worked like a charm to put the new short description up above the long description. Thank you!

function WH_remove_meta_boxes() {
        remove_meta_box( 'postexcerpt', 'product', 'normal' );      
        remove_meta_box( 'tagsdiv-product_tag', 'product', 'side' );    
        remove_meta_box( 'tagsdiv-yith_shop_vendor', 'product', 'side' );   
        remove_meta_box( 'tagsdiv-product_tag', 'product', 'side' );
        remove_meta_box( 'wpseo_meta', 'product', 'normal');        
    
}
add_action( 'add_meta_boxes', 'WH_remove_meta_boxes', 99 ); 
0

Answers outdated for guttenberg.

function wpdocs_remove_page_excerpt_field() {
    remove_post_type_support( 'post', 'excerpt' );
}
add_action( 'admin_init', 'wpdocs_remove_page_excerpt_field' );

You must first remove post excerpt support and then add your custom excerpt meta

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.