wppaste
WordPress

wp_ajax_save_attachment()

Since
3.5.0
Source
wp-admin/includes/ajax-actions.php:3140
Handles updating attachment attributes via AJAX.

Compatibility

WordPress
since 3.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to wp_ajax_save_attachment() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
62–132

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 164.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksdo_action()one call below wp_ajax_save_attachment()

Further down the call graph this can also reach option, serialize, cache and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 18 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_ajax_save_attachment() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!isset($value) && current_user_can() && !isset($changes) && !wp_attachment_is()62–81wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_attachment_is(), wp_update_post(), wp_send_json_success()
!isset($value) && current_user_can() && !wp_attachment_is()68–82wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_attachment_is(), wp_delete_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt === false && !wp_attachment_is()75–94wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_attachment_is(), wp_update_post(), wp_send_json_success()
!isset($value) && current_user_can() && !isset($changes) && wp_attachment_is()79–101wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_update_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt === false && !wp_attachment_is()81–95wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_attachment_is(), wp_delete_post(), wp_send_json_success()
!isset($value) && current_user_can() && !isset($changes) && wp_attachment_is()83–105wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_update_attachment_metadata(), wp_update_post(), wp_send_json_success()
!isset($value) && current_user_can() && wp_attachment_is()85–102wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_delete_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt !== false && !wp_attachment_is()88–107wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), wp_attachment_is(), wp_update_post(), wp_send_json_success()
!isset($value) && current_user_can() && wp_attachment_is()89–106wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_update_attachment_metadata(), wp_delete_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt === false && wp_attachment_is()92–114wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_update_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt !== false && !wp_attachment_is()94–108wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), wp_attachment_is(), wp_delete_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt === false && wp_attachment_is()96–118wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_update_attachment_metadata(), wp_update_post(), wp_send_json_success()
6 further outcomes, up to 132 instructions
!isset($value) && current_user_can() && $alt === false && wp_attachment_is()98–115wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_delete_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt === false && wp_attachment_is()102–119wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_update_attachment_metadata(), wp_delete_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt !== false && wp_attachment_is()105–127wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_update_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt !== false && wp_attachment_is()109–131wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_update_attachment_metadata(), wp_update_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt !== false && wp_attachment_is()111–128wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_delete_post(), wp_send_json_success()
!isset($value) && current_user_can() && $alt !== false && wp_attachment_is()115–132wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_unslash(), get_post_meta(), wp_strip_all_tags(), wp_slash(), update_post_meta(), wp_attachment_is(), wp_get_attachment_metadata(), wp_get_attachment_id3_keys(), wp_update_attachment_metadata(), wp_delete_post(), wp_send_json_success()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 164 instructions, 62–132 executed per call, 23 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 18

Show all 18

Source code

function wp_ajax_save_attachment() {	if ( ! isset( $_REQUEST['id'] ) || ! isset( $_REQUEST['changes'] ) ) {		wp_send_json_error();	} 	$id = absint( $_REQUEST['id'] );	if ( ! $id ) {		wp_send_json_error();	} 	check_ajax_referer( 'update-post_' . $id, 'nonce' ); 	if ( ! current_user_can( 'edit_post', $id ) ) {		wp_send_json_error();	} 	$changes = $_REQUEST['changes'];	$post    = get_post( $id, ARRAY_A );	if ( ! $post ) {		wp_send_json_error();	} 	if ( 'attachment' !== $post['post_type'] ) {		wp_send_json_error();	} 	if ( isset( $changes['parent'] ) ) {		$post['post_parent'] = $changes['parent'];	} 	if ( isset( $changes['title'] ) ) {		$post['post_title'] = $changes['title'];	} 	if ( isset( $changes['caption'] ) ) {		$post['post_excerpt'] = $changes['caption'];	} 	if ( isset( $changes['description'] ) ) {		$post['post_content'] = $changes['description'];	} 	if ( MEDIA_TRASH && isset( $changes['status'] ) ) {		$post['post_status'] = $changes['status'];	} 	if ( isset( $changes['alt'] ) ) {		$alt = wp_unslash( $changes['alt'] );		if ( get_post_meta( $id, '_wp_attachment_image_alt', true ) !== $alt ) {			$alt = wp_strip_all_tags( $alt, true );			update_post_meta( $id, '_wp_attachment_image_alt', wp_slash( $alt ) );		}	} 	if ( wp_attachment_is( 'audio', $post['ID'] ) ) {		$changed  = false;		$id3_data = wp_get_attachment_metadata( $post['ID'] ); 		if ( ! is_array( $id3_data ) ) {			$changed  = true;			$id3_data = array();		} 		foreach ( wp_get_attachment_id3_keys( (object) $post, 'edit' ) as $key => $label ) {			if ( isset( $changes[ $key ] ) ) {				$changed          = true;				$id3_data[ $key ] = sanitize_text_field( wp_unslash( $changes[ $key ] ) );			}		} 		if ( $changed ) {			wp_update_attachment_metadata( $id, $id3_data );		}	} 	if ( MEDIA_TRASH && isset( $changes['status'] ) && 'trash' === $changes['status'] ) {		wp_delete_post( $id );	} else {		wp_update_post( $post );	}

Changelog

Introduced in 3.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-admin/includes/ajax-actions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.