Title: wp_ajax_save_attachment_compat
Published: April 25, 2014
Last modified: February 24, 2026

---

# wp_ajax_save_attachment_compat()

## In this article

 * [Source](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#wp--skip-link--target)

Handles saving backward compatible attachment attributes via AJAX.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#source)󠁿

    ```php
    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 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/ajax-actions.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/ajax-actions.php#L3198)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/ajax-actions.php#L3198-L3249)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#hooks)󠁿

 [apply_filters( ‘attachment_fields_to_save’, array $post, array $attachment )](https://developer.wordpress.org/reference/hooks/attachment_fields_to_save/)

Filters the attachment fields to be saved.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_set_object_terms()](https://developer.wordpress.org/reference/functions/wp_set_object_terms/)`wp-includes/taxonomy.php` |

Creates term and taxonomy relationships.

  | 
| [wp_prepare_attachment_for_js()](https://developer.wordpress.org/reference/functions/wp_prepare_attachment_for_js/)`wp-includes/media.php` |

Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.

  | 
| [get_attachment_taxonomies()](https://developer.wordpress.org/reference/functions/get_attachment_taxonomies/)`wp-includes/media.php` |

Retrieves taxonomies attached to given the attachment.

  | 
| [wp_update_post()](https://developer.wordpress.org/reference/functions/wp_update_post/)`wp-includes/post.php` |

Updates a post with new post data.

  | 
| [current_user_can()](https://developer.wordpress.org/reference/functions/current_user_can/)`wp-includes/capabilities.php` |

Returns whether the current user has the specified capability.

  | 
| [check_ajax_referer()](https://developer.wordpress.org/reference/functions/check_ajax_referer/)`wp-includes/pluggable.php` |

Verifies the Ajax request to prevent processing requests external of the blog.

  | 
| [wp_send_json_error()](https://developer.wordpress.org/reference/functions/wp_send_json_error/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating failure.

  | 
| [absint()](https://developer.wordpress.org/reference/functions/absint/)`wp-includes/load.php` |

Converts a value to non-negative integer.

  | 
| [wp_send_json_success()](https://developer.wordpress.org/reference/functions/wp_send_json_success/)`wp-includes/functions.php` |

Sends a JSON response back to an Ajax request, indicating success.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  | 
| [get_post()](https://developer.wordpress.org/reference/functions/get_post/)`wp-includes/post.php` |

Retrieves post data given a post ID or post object.

  |

[Show 7 more](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_ajax_save_attachment_compat/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.5.0](https://developer.wordpress.org/reference/since/3.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_ajax_save_attachment_compat%2F)
before being able to contribute a note or feedback.