4

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.

2
  • 2
    Please post a better question, no one is going to read through a pastebin of 600 lines of code. Code should be posted here in WPSE and in minimal format for the actual problem.
    – Wyck
    Commented Aug 28, 2013 at 21:07
  • 1
    I've shortened the code and just posted the bits I think may be the source of the problem, but the question is pretty straight forward. Like I said WP is not saving the position of my metabox fields when they are re-positioned via jQuery's sortable function like it was in 3.5.
    – luxo
    Commented Aug 28, 2013 at 21:48

1 Answer 1

1

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:

add_action( 'admin_enqueue_scripts', 'wpse112022_postbox_enqueue' );
function wpse112022_postbox_enqueue( $hook_suffix )
{
    // uncomment the next line to see what the $hook_suffix of your desired page is
    # var_dump( $hook_suffix );
    if ( 'hook_suffix of desired page' !== $hook_suffix )
        return;

    wp_enqueue_script( 'postbox' );
    // If the last line didn't enqueue the script, then uncomment the following instead:
    /*
    $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
    wp_enqueue_script(
        'postbox',
        "/wp-admin/js/postbox{$suffix}.js",
        array( 'jquery-ui-sortable' ),
        false
        true
    );
    */
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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