wp_ajax_save_attachment()
- Since
- 3.5.0
- Source
wp-admin/includes/ajax-actions.php:3140
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
- Scaling
- Scales with input
- Instructions
- 62–132
- Plugin surface
- None
- Called by
- 0
Reaches the database via get_post().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 164.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
do_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.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($value) && current_user_can() && !isset($changes) && !wp_attachment_is() | 62–81 | wp_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–82 | wp_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–94 | wp_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–101 | wp_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–95 | wp_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–105 | wp_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–102 | wp_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–107 | wp_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–106 | wp_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–114 | wp_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–108 | wp_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–118 | wp_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–115 | wp_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–119 | wp_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–127 | wp_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–131 | wp_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–128 | wp_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–132 | wp_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
- wp_send_json_error()Sends a JSON response back to an Ajax request, indicating failure.
- absint()Converts a value to non-negative integer.
- check_ajax_referer()Verifies the Ajax request to prevent processing requests external of the blog.
- current_user_can()Returns whether the current user has the specified capability.
- get_post()Retrieves post data given a post ID or post object.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- get_post_meta()Retrieves a post meta field for the given post ID.
- wp_strip_all_tags()Properly strips all HTML tags including 'script' and 'style'.
- update_post_meta()Updates a post meta field based on the given post ID.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- wp_attachment_is()Verifies an attachment is of a given type.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
Show all 18
- wp_get_attachment_id3_keys()Returns useful keys to use to lookup data from an attachment's stored metadata.
- sanitize_text_field()Sanitizes a string from user input or from the database.
- wp_update_attachment_metadata()Updates metadata for an attachment.
- wp_delete_post()Trashes or deletes a post or page.
- wp_update_post()Updates a post with new post data.
- wp_send_json_success()Sends a JSON response back to an Ajax request, indicating success.
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.
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.