WordPress AJAX drag-and-drop images in gallery are not retaining the order set by the user

Working on some code put together by a previous web developer and I’m running into an issue where updating the drag-and-drop AJAX-driven photo order of a gallery is not saving. I am running the latest version of WordPress (3.3.2) but the client says that custom ordering has never worked.

Here’s a more detailed list of what’s occuring:

Read More
  1. Client goes to the edit area of a post on the WP admin panel.
  2. Client uses the drag-and-drop feature to reorder some photos and an AJAX-triggered alert says that the order has been saved.
  3. The user clicks the “Update” button and views their post. The order is now jumbled and has not retained their drag-and-drop order.

The method below worked for me but only partially:

  1. Clicked on an existing post to edit it.
  2. Dragged and dropped the arrangement of some photos but did not click “Update” yet.
  3. Instead, after the AJAX-driven alert displayed stating that the order saved, I went and refreshed the updated post on the front end. The post reflected the new photo order that I had set.
  4. After avoiding the “Update” button like the plague, I clicked on the post list and went back into the same post like I wanted to edit something else. Basically starting a new session within the post itself I guess. The photo order is now jumbled up and the drag-and-drop order I had previously set is blown away.

Below is the code in the functions.php file that is handling the photo order itself:

// Ajax callback for reordering images
function reorder_images() {
    if (!isset($_POST['data'])) die();

    list($order, $post_id, $key, $nonce) = explode('!',$_POST['data']);

    if (!wp_verify_nonce($nonce, 'rw_ajax_sort_file')) {
        die('1');
    }

    parse_str($order, $items);
    $items = $items['item'];
    $order = 0;
    $meta = array();
    foreach ($items as $item) {
        wp_update_post(array(
            'ID' => $item,
            'post_parent' => $post_id,
            'menu_order' => $order
        ));
        $order++;
        $meta[] = $item;
    }
    delete_post_meta($post_id, $key);
    foreach ($meta as $value) {
        add_post_meta($post_id, $key, $value);
    }

    die('0');
}

I was thinking that there was an issue with DOING_AUTOSAVE but found that it was already implemented in the code (see below):

// Save data from meta box
function save($post_id) {
    global $post_type;
    $post_type_object = get_post_type_object($post_type);

    if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)                       // check autosave
    || (!isset($_POST['post_ID']) || $post_id != $_POST['post_ID'])         // check revision
    || (!in_array($post_type, $this->_meta_box['pages']))                   // check if current post type is supported
    || (!check_admin_referer(basename(__FILE__), 'rw_meta_box_nonce'))      // verify nonce
    || (!current_user_can($post_type_object->cap->edit_post, $post_id))) {  // check permission
        return $post_id;
    }

    foreach ($this->_fields as $field) {
        $name = $field['id'];
        $type = $field['type'];
        $old = get_post_meta($post_id, $name, !$field['multiple']);
        $new = isset($_POST[$name]) ? $_POST[$name] : ($field['multiple'] ? array() : '');

        // validate meta value
        if (class_exists('RW_Meta_Box_Validate') && method_exists('RW_Meta_Box_Validate', $field['validate_func'])) {
            $new = call_user_func(array('RW_Meta_Box_Validate', $field['validate_func']), $new);
        }

        // call defined method to save meta value, if there's no methods, call common one
        $save_func = 'save_field_' . $type;
        if (method_exists($this, $save_func)) {
            call_user_func(array(&$this, 'save_field_' . $type), $post_id, $field, $old, $new);
        } else {
            $this->save_field($post_id, $field, $old, $new);
        }
    }
}

If you want to see what was implemented in its entirety, you can see it here.

Thanks in advance. If you need more information, I’ll try to supply everything I can. Perhaps it’s simply a plugin conflict of some sort. The method outlined above is currently being implemented via the functions.php file in the theme itself.

Related posts

Leave a Reply