Skip to content

Adding Fields After the Title (Above Main WP Content Editor)

Jake edited this page May 22, 2015 · 4 revisions

Adding FM fields above the WP main content editor is a fairly simple process that can be broken down into three basic steps:

  1. Render the meta boxes to appear in some uniquely defined context--(uniquely defined context as in not 'normal', 'side', or 'advanced'). Here, 'ai_after_title' is used.
  2. unset the unique context from $wp_meta_boxes['post'].
  3. Set the $context argument of any $fm->add_meta_box() calls to match the unique context from above.

The hook, edit_form_after_title is used to achieve the first and second steps.

Set up the context:

function ai_edit_form_after_title() {
	// get globals vars
	global $post, $wp_meta_boxes;

	// render the FM meta box in 'ai_after_title' context
	do_meta_boxes( get_current_screen(), 'ai_after_title', $post );

	// unset 'ai_after_title' context from the post's meta boxes
	unset( $wp_meta_boxes['post']['ai_after_title'] );
}
add_action( 'edit_form_after_title', 'ai_edit_form_after_title' );

Use the context:

function ai_after_title_fm_fields() {
	$fm = new Fieldmanager_Group( array(
		'name'     => 'after_title_fm_fields',
		'children' => array(
			'After Title Text' => new Fieldmanager_TextField( __( 'After Title Text', 'alleyinteractive' ) ),
			'After Title Link' => new Fieldmanager_Link( __( 'After Title Link', 'alleyinteractive' ) ),
		),
	) );

	$fm->add_meta_box( __( 'AI After Title', 'alleyinteractive' ), array( 'post' ), 'ai_after_title', 'high' );
}
add_action( 'fm_post_post', 'ai_after_title_fm_fields' );

Once executed properly, you should be able to now drag other draggable meta boxes in and out of this context.