wp_ajax_save_attachment_order() WordPress Function

The wp_ajax_save_attachment_order() function is used to save the order in which attachments are displayed on a post. This function is triggered when the user clicks the "Save Order" button on the Attachment Order page.

wp_ajax_save_attachment_order() #

Ajax handler for saving the attachment order.


Source

File: wp-admin/includes/ajax-actions.php

3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
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();
}


Top ↑

Changelog

Changelog
VersionDescription
3.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More