Skip to content

Instantly share code, notes, and snippets.

@tddewey
Created October 9, 2012 23:53
Show Gist options
  • Save tddewey/3862239 to your computer and use it in GitHub Desktop.
Save tddewey/3862239 to your computer and use it in GitHub Desktop.
Moving meta boxes with WP_Content editors
/**
* Moves meta boxes in the 'normal' context to the front of the 'advanced' context.
* To be used when inserting content via the edit_form_advanced hook (example given)
*/
class MoveMetaBoxes {
public $CPT_Slug = 'tdd_library';
function __construct() {
add_action( 'edit_form_advanced', array( $this, 'edit_form_advanced' ), 1 );
add_filter( "get_user_option_meta-box-order_$this->CPT_Slug", array( $this, 'move_meta_boxes_to_advanced' ), 10, 3 );
// This is the only way to get cascading priority, but really should be more targeted to the CPT_Slug. Further investigation needed
add_action( 'admin_head', array( $this, 'collapse_normal_sortables_area' ) );
}
/**
* Filters the sort order of meta boxes.
* This is run just before they output for the first time. We will remove all the boxes
* From 'normal' and add them to 'advanced'
* Advanced metaboxes shows up below 'edit_form_advanced'
* @param $order array array for each meta box context with a string of meta box ids
* @param $param
* @param $user user object
* @return array with the modified order and an empty 'normal' key
*/
function move_meta_boxes_to_advanced( $order, $param, $user ) {
if (isset( $order['normal'] )) {
$order['advanced'] = join( ',', array( $order['normal'], $order['advanced'] ) );
$order['normal'] = '';
}
return $order;
}
/**
* Override the minimum height for sortable areas
* Even if there are no metaboxes, there is still a min-height of 50px so
* that there is a drag target. We don't want anyone to drag stuff here, but
* We'll not display:none it just in case a metabox sneaks in there via
* some seriously messed up code.
* @return void
*/
function collapse_normal_sortables_area( ) {
?>
<style type="text/css">
#post-body #normal-sortables { min-height: 0 }
</style>
<?php
}
function edit_form_advanced() {
global $post;
if ( $post->post_type == $this->CPT_Slug ) {
wp_editor( 'default content', 'editor_id' );
}
}
}
$move_meta_boxes = new MoveMetaBoxes();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment