11

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?

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');
  • 1
    Hey +1 on that, but how do you address the styling after the removal of the meta_box? – DᴀʀᴛʜVᴀᴅᴇʀ Sep 22 '14 at 2:24
4

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' );
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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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