wp_ajax_save_attachment_order

The timeline below displays how wordpress function wp_ajax_save_attachment_order has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Handles saving the attachment order via AJAX.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment_order()
{
    if (!isset($_REQUEST['post_id'])) {
        wp_send_json_error();
    }
    $post_id = absint($_REQUEST['post_id']);
    if (!$post_id) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments'])) {
        wp_send_json_error();
    }
    check_ajax_referer('update-post_' . $post_id, 'nonce');
    $attachments = $_REQUEST['attachments'];
    if (!current_user_can('edit_post', $post_id)) {
        wp_send_json_error();
    }
    foreach ($attachments as $attachment_id => $menu_order) {
        if (!current_user_can('edit_post', $attachment_id)) {
            continue;
        }
        $attachment = get_post($attachment_id);
        if (!$attachment) {
            continue;
        }
        if ('attachment' !== $attachment->post_type) {
            continue;
        }
        wp_update_post(array('ID' => $attachment_id, 'menu_order' => $menu_order));
    }
    wp_send_json_success();
}

WordPress Version: 5.5

/**
 * Ajax handler for saving the attachment order.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment_order()
{
    if (!isset($_REQUEST['post_id'])) {
        wp_send_json_error();
    }
    $post_id = absint($_REQUEST['post_id']);
    if (!$post_id) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments'])) {
        wp_send_json_error();
    }
    check_ajax_referer('update-post_' . $post_id, 'nonce');
    $attachments = $_REQUEST['attachments'];
    if (!current_user_can('edit_post', $post_id)) {
        wp_send_json_error();
    }
    foreach ($attachments as $attachment_id => $menu_order) {
        if (!current_user_can('edit_post', $attachment_id)) {
            continue;
        }
        $attachment = get_post($attachment_id);
        if (!$attachment) {
            continue;
        }
        if ('attachment' !== $attachment->post_type) {
            continue;
        }
        wp_update_post(array('ID' => $attachment_id, 'menu_order' => $menu_order));
    }
    wp_send_json_success();
}

WordPress Version: 5.3

/**
 * Ajax handler for saving the attachment order.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment_order()
{
    if (!isset($_REQUEST['post_id'])) {
        wp_send_json_error();
    }
    $post_id = absint($_REQUEST['post_id']);
    if (!$post_id) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments'])) {
        wp_send_json_error();
    }
    check_ajax_referer('update-post_' . $post_id, 'nonce');
    $attachments = $_REQUEST['attachments'];
    if (!current_user_can('edit_post', $post_id)) {
        wp_send_json_error();
    }
    foreach ($attachments as $attachment_id => $menu_order) {
        if (!current_user_can('edit_post', $attachment_id)) {
            continue;
        }
        $attachment = get_post($attachment_id);
        if (!$attachment) {
            continue;
        }
        if ('attachment' != $attachment->post_type) {
            continue;
        }
        wp_update_post(array('ID' => $attachment_id, 'menu_order' => $menu_order));
    }
    wp_send_json_success();
}

WordPress Version: 4.0

/**
 * Ajax handler for saving the attachment order.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment_order()
{
    if (!isset($_REQUEST['post_id'])) {
        wp_send_json_error();
    }
    if (!$post_id = absint($_REQUEST['post_id'])) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments'])) {
        wp_send_json_error();
    }
    check_ajax_referer('update-post_' . $post_id, 'nonce');
    $attachments = $_REQUEST['attachments'];
    if (!current_user_can('edit_post', $post_id)) {
        wp_send_json_error();
    }
    foreach ($attachments as $attachment_id => $menu_order) {
        if (!current_user_can('edit_post', $attachment_id)) {
            continue;
        }
        if (!$attachment = get_post($attachment_id)) {
            continue;
        }
        if ('attachment' != $attachment->post_type) {
            continue;
        }
        wp_update_post(array('ID' => $attachment_id, 'menu_order' => $menu_order));
    }
    wp_send_json_success();
}

WordPress Version: 3.7

function wp_ajax_save_attachment_order()
{
    if (!isset($_REQUEST['post_id'])) {
        wp_send_json_error();
    }
    if (!$post_id = absint($_REQUEST['post_id'])) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments'])) {
        wp_send_json_error();
    }
    check_ajax_referer('update-post_' . $post_id, 'nonce');
    $attachments = $_REQUEST['attachments'];
    if (!current_user_can('edit_post', $post_id)) {
        wp_send_json_error();
    }
    $post = get_post($post_id, ARRAY_A);
    foreach ($attachments as $attachment_id => $menu_order) {
        if (!current_user_can('edit_post', $attachment_id)) {
            continue;
        }
        if (!$attachment = get_post($attachment_id)) {
            continue;
        }
        if ('attachment' != $attachment->post_type) {
            continue;
        }
        wp_update_post(array('ID' => $attachment_id, 'menu_order' => $menu_order));
    }
    wp_send_json_success();
}