wp_ajax_save_attachment_compat

The timeline below displays how wordpress function wp_ajax_save_attachment_compat 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 backward compatible attachment attributes via AJAX.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment_compat()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    $id = absint($_REQUEST['id']);
    if (!$id) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments']) || empty($_REQUEST['attachments'][$id])) {
        wp_send_json_error();
    }
    $attachment_data = $_REQUEST['attachments'][$id];
    check_ajax_referer('update-post_' . $id, 'nonce');
    if (!current_user_can('edit_post', $id)) {
        wp_send_json_error();
    }
    $post = get_post($id, ARRAY_A);
    if ('attachment' !== $post['post_type']) {
        wp_send_json_error();
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $post = apply_filters('attachment_fields_to_save', $post, $attachment_data);
    if (isset($post['errors'])) {
        $errors = $post['errors'];
        // @todo return me and display me!
        unset($post['errors']);
    }
    wp_update_post($post);
    foreach (get_attachment_taxonomies($post) as $taxonomy) {
        if (isset($attachment_data[$taxonomy])) {
            wp_set_object_terms($id, array_map('trim', preg_split('/,+/', $attachment_data[$taxonomy])), $taxonomy, false);
        }
    }
    $attachment = wp_prepare_attachment_for_js($id);
    if (!$attachment) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}

WordPress Version: 5.5

/**
 * Ajax handler for saving backward compatible attachment attributes.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment_compat()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    $id = absint($_REQUEST['id']);
    if (!$id) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments']) || empty($_REQUEST['attachments'][$id])) {
        wp_send_json_error();
    }
    $attachment_data = $_REQUEST['attachments'][$id];
    check_ajax_referer('update-post_' . $id, 'nonce');
    if (!current_user_can('edit_post', $id)) {
        wp_send_json_error();
    }
    $post = get_post($id, ARRAY_A);
    if ('attachment' !== $post['post_type']) {
        wp_send_json_error();
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $post = apply_filters('attachment_fields_to_save', $post, $attachment_data);
    if (isset($post['errors'])) {
        $errors = $post['errors'];
        // @todo return me and display me!
        unset($post['errors']);
    }
    wp_update_post($post);
    foreach (get_attachment_taxonomies($post) as $taxonomy) {
        if (isset($attachment_data[$taxonomy])) {
            wp_set_object_terms($id, array_map('trim', preg_split('/,+/', $attachment_data[$taxonomy])), $taxonomy, false);
        }
    }
    $attachment = wp_prepare_attachment_for_js($id);
    if (!$attachment) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}

WordPress Version: 5.3

/**
 * Ajax handler for saving backward compatible attachment attributes.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment_compat()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    $id = absint($_REQUEST['id']);
    if (!$id) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments']) || empty($_REQUEST['attachments'][$id])) {
        wp_send_json_error();
    }
    $attachment_data = $_REQUEST['attachments'][$id];
    check_ajax_referer('update-post_' . $id, 'nonce');
    if (!current_user_can('edit_post', $id)) {
        wp_send_json_error();
    }
    $post = get_post($id, ARRAY_A);
    if ('attachment' != $post['post_type']) {
        wp_send_json_error();
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $post = apply_filters('attachment_fields_to_save', $post, $attachment_data);
    if (isset($post['errors'])) {
        $errors = $post['errors'];
        // @todo return me and display me!
        unset($post['errors']);
    }
    wp_update_post($post);
    foreach (get_attachment_taxonomies($post) as $taxonomy) {
        if (isset($attachment_data[$taxonomy])) {
            wp_set_object_terms($id, array_map('trim', preg_split('/,+/', $attachment_data[$taxonomy])), $taxonomy, false);
        }
    }
    $attachment = wp_prepare_attachment_for_js($id);
    if (!$attachment) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}

WordPress Version: 4.6

/**
 * Ajax handler for saving backward compatible attachment attributes.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment_compat()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    if (!$id = absint($_REQUEST['id'])) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments']) || empty($_REQUEST['attachments'][$id])) {
        wp_send_json_error();
    }
    $attachment_data = $_REQUEST['attachments'][$id];
    check_ajax_referer('update-post_' . $id, 'nonce');
    if (!current_user_can('edit_post', $id)) {
        wp_send_json_error();
    }
    $post = get_post($id, ARRAY_A);
    if ('attachment' != $post['post_type']) {
        wp_send_json_error();
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $post = apply_filters('attachment_fields_to_save', $post, $attachment_data);
    if (isset($post['errors'])) {
        $errors = $post['errors'];
        // @todo return me and display me!
        unset($post['errors']);
    }
    wp_update_post($post);
    foreach (get_attachment_taxonomies($post) as $taxonomy) {
        if (isset($attachment_data[$taxonomy])) {
            wp_set_object_terms($id, array_map('trim', preg_split('/,+/', $attachment_data[$taxonomy])), $taxonomy, false);
        }
    }
    if (!$attachment = wp_prepare_attachment_for_js($id)) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}

WordPress Version: 4.0

/**
 * Ajax handler for saving backwards compatible attachment attributes.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment_compat()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    if (!$id = absint($_REQUEST['id'])) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments']) || empty($_REQUEST['attachments'][$id])) {
        wp_send_json_error();
    }
    $attachment_data = $_REQUEST['attachments'][$id];
    check_ajax_referer('update-post_' . $id, 'nonce');
    if (!current_user_can('edit_post', $id)) {
        wp_send_json_error();
    }
    $post = get_post($id, ARRAY_A);
    if ('attachment' != $post['post_type']) {
        wp_send_json_error();
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $post = apply_filters('attachment_fields_to_save', $post, $attachment_data);
    if (isset($post['errors'])) {
        $errors = $post['errors'];
        // @todo return me and display me!
        unset($post['errors']);
    }
    wp_update_post($post);
    foreach (get_attachment_taxonomies($post) as $taxonomy) {
        if (isset($attachment_data[$taxonomy])) {
            wp_set_object_terms($id, array_map('trim', preg_split('/,+/', $attachment_data[$taxonomy])), $taxonomy, false);
        }
    }
    if (!$attachment = wp_prepare_attachment_for_js($id)) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}

WordPress Version: 3.7

/**
 * Save backwards compatible attachment attributes.
 *
 * @since 3.5.0
 */
function wp_ajax_save_attachment_compat()
{
    if (!isset($_REQUEST['id'])) {
        wp_send_json_error();
    }
    if (!$id = absint($_REQUEST['id'])) {
        wp_send_json_error();
    }
    if (empty($_REQUEST['attachments']) || empty($_REQUEST['attachments'][$id])) {
        wp_send_json_error();
    }
    $attachment_data = $_REQUEST['attachments'][$id];
    check_ajax_referer('update-post_' . $id, 'nonce');
    if (!current_user_can('edit_post', $id)) {
        wp_send_json_error();
    }
    $post = get_post($id, ARRAY_A);
    if ('attachment' != $post['post_type']) {
        wp_send_json_error();
    }
    /** This filter is documented in wp-admin/includes/media.php */
    $post = apply_filters('attachment_fields_to_save', $post, $attachment_data);
    if (isset($post['errors'])) {
        $errors = $post['errors'];
        // @todo return me and display me!
        unset($post['errors']);
    }
    wp_update_post($post);
    foreach (get_attachment_taxonomies($post) as $taxonomy) {
        if (isset($attachment_data[$taxonomy])) {
            wp_set_object_terms($id, array_map('trim', preg_split('/,+/', $attachment_data[$taxonomy])), $taxonomy, false);
        }
    }
    if (!$attachment = wp_prepare_attachment_for_js($id)) {
        wp_send_json_error();
    }
    wp_send_json_success($attachment);
}