How do I position meta_box on post edit screen after the title?

I am displaying a div in the Edit Post screen using add_meta_box(). This function provides very limited positioning options via “priority” and “context” params and these are not sufficient for my needs.

I need to be able to display the div below the Permalink but above the Insert Media button on Edit Post, specifically below the div “titlediv”, above the div “postdivrich”.

How else can I position the meta box on the post edit screen? Can this be done through jQuery?

Here’s a partially working solution:

function admin_init(){
  add_meta_box("wd_meta", "WD Meta", "wd_meta", "post", "normal", "high");
}
add_action("admin_init", "admin_init");

function wd_meta() {
    global $post;
    $custom = get_post_custom($post->ID);
?>
<div id="wdMeta">
<p>Display a message here.</p>
</div> 

<script>
$('#wd_meta').insertAfter('#titlediv'); 
</script>
<?php } ?>

The meta box is displaying on the page correctly, but the javascript is not positioning it correctly, even though it appears to work in demo:

http://jsfiddle.net/kFTc5/11/

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You cannot use a real metabox to do that, hook into edit_form_after_title instead.

enter image description here

Here is a simple example:

add_action( 'edit_form_after_title', 'wpse_87478_pseudo_metabox' );
add_action( 'save_post', 'wpse_87478_save_metabox' );

function wpse_87478_pseudo_metabox()
{
    global $post;
    $key = '_wpse_87478';

    if ( empty ( $post ) || 'post' !== get_post_type( $GLOBALS['post'] ) )
        return;

    if ( ! $content = get_post_meta( $post->ID, $key, TRUE ) )
        $content = '';

    printf(
        '<p><label for="%1$s_id">Enter some text
        <input type="text" name="%1$s" id="%1$s_id" value="%2$s" class="all-options" />
        </label></p>',
        $key,
        esc_attr( $content )
    );
}

function wpse_87478_save_metabox( $post_id )
{
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return;

    $key = '_wpse_87478';

    if ( isset ( $_POST[ $key ] ) )
        return update_post_meta( $post_id, $key, $_POST[ $key ] );

    delete_post_meta( $post_id, $key );
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x