Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Right now, the Title is the first thing at the top of the page, then we have next the WYSIWYG Editor.

I would like to add a Custom Meta Box and insert it before the title or WYSIWYG editor.
How can I move them around?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

There is a way to put that meta box after the title. There is a hook called edit_form_after_title. If you look at the source there is no hook earlier than that that isn't much too early-- outside of the <form tag. The following does move that box to the location after the title...

function generic_box() {
  remove_meta_box('postcustom',null,'core');
  add_meta_box('postcustomv2', __('Custom Fields'), 'post_custom_meta_box', 'post', 'after_title');
}
add_action('do_meta_boxes','generic_box',100);

function add_box_after_title() {
  global $post_type, $post;
  do_meta_boxes($post_type, 'after_title', $post);
}
add_action( 'edit_form_after_title', 'add_box_after_title' );

... but there is a slight issue. I can't seem to get the box to remove and then add it back with the same ID. I've added it back as postcustomv2 but it is the same callback. This results in a slight formatting glitch but it seems to work.

Edit:

I thought of a way to cheat... a lot. No guarantees of functionality with this one.

function title_cheat() { 
  global $post; ?>
  <div id="titlewrap">
    <label class="screen-reader-text" id="title-prompt-text" for="title"><?php echo apply_filters( 'enter_title_here', __( 'Enter title here' ), $post ); ?></label>
    <input type="text" name="post_title" size="30" value="<?php echo esc_attr( htmlspecialchars( $post->post_title ) ); ?>" id="title" autocomplete="off" />
 </div><?php
}

function generic_box() {
  remove_meta_box('postcustom',null,'core');
  remove_post_type_support('post','title');   
  add_meta_box('postcustomv2', __('Custom Fields'), 'post_custom_meta_box', 'post', 'after_title');
  add_meta_box(
    'titlediv', // id, used as the html id att
    __( 'Title' ), // meta box title
    'title_cheat', // callback function, spits out the content
    'post', // post type or page. This adds to posts only
    'after_title', // context, where on the screen
    'low' // priority, where should this go in the context
  );
}
add_action('do_meta_boxes','generic_box',100);

function add_box_after_title() {
  global $post_type, $post;
  do_meta_boxes($post_type, 'after_title', $post);
}
add_action( 'edit_form_after_title', 'add_box_after_title' );

If you remove post type support for the "title" the title box disappears, and you can then add it back where you want.

Barely tested. Possibly buggy. Caveat emptor. No refunds.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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