I’ve run into a problem that has got me stumped: after upgrading to WP 3.6 my sortable metabox fields are not saving their position when you re-order them. Below is my code:
PHP:
function save_box( $post_id ) {
$post_type = get_post_type();
// verify nonce
if ( ! isset( $_POST['custom_meta_box_nonce_field'] ) )
return $post_id;
if ( ! ( in_array( $post_type, $this->page ) || wp_verify_nonce( $_POST['custom_meta_box_nonce_field'], 'custom_meta_box_nonce_action' ) ) )
return $post_id;
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// check permissions
if ( ! current_user_can( 'edit_page', $post_id ) )
return $post_id;
// loop through fields and save the data
foreach ( $this->fields as $field ) {
if( $field['type'] == 'section' ) {
$sanitizer = null;
continue;
}
if( in_array( $field['type'], array( 'tax_select', 'tax_checkboxes' ) ) ) {
// save taxonomies
if ( isset( $_POST[$field['id']] ) ) {
$term = $_POST[$field['id']];
wp_set_object_terms( $post_id, $term, $field['id'] );
}
}
else {
// save the rest
$old = get_post_meta( $post_id, $field['id'], $field['type'], true );
if ( isset( $_POST[$field['id']] ) )
$new = $_POST[$field['id']];
if ( isset( $new ) && $new != $old ) {
$sanitizer = isset( $field['sanitizer'] ) ? $field['sanitizer'] : 'sanitize_text_field';
if ( is_array( $new ) )
$new = meta_box_array_map_r( 'meta_box_sanitize', $new, $sanitizer );
else
$new = meta_box_sanitize( $new, $sanitizer );
update_post_meta( $post_id, $field['id'], $new );
} elseif ( isset( $new ) && '' == $new && $old ) {
delete_post_meta( $post_id, $field['id'], $old );
}
}
} // end foreach
}
jQuery:
$('.meta_box_repeatable tbody').sortable({
opacity: 0.6,
revert: true,
cursor: 'move',
handle: '.hndle'
});
// post_drop_sort
$('.sort_list').sortable({
connectWith: '.sort_list',
opacity: 0.6,
revert: true,
cursor: 'move',
cancel: '.post_drop_sort_area_name',
items: 'li:not(.post_drop_sort_area_name)',
update: function(event, ui) {
var result = $(this).sortable('toArray');
var thisID = $(this).attr('id');
$('.store-' + thisID).val(result)
}
});
$('.sort_list').disableSelection();
What I have is a metabox that contains a repeatable box, inside the repeatable box is an image upload button and caption text input. Everything works fine, I am able to add more repeatables and sort them, but when I save the post the repeatables go back to their original position.
If anyone would like I can give you login details to see the live version.
Any help would be greatly appreciated.
Normally it should be enough to simply enqueue the
postsbox
script on a page where you want to have sortable meta boxes.Simply do the following: