wp_ajax_save_attachment

Ajax handler for updating attachment attributes.

Description

wp_ajax_save_attachment(); 

Usage

  1. if ( !function_exists( 'wp_ajax_save_attachment' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/ajax-actions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_ajax_save_attachment(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_save_attachment() { 
  2. if ( ! isset( $_REQUEST['id'] ) || ! isset( $_REQUEST['changes'] ) ) 
  3.  
  4. if ( ! $id = absint( $_REQUEST['id'] ) ) 
  5.  
  6. check_ajax_referer( 'update-post_' . $id, 'nonce' ); 
  7.  
  8. if ( ! current_user_can( 'edit_post', $id ) ) 
  9.  
  10. $changes = $_REQUEST['changes']; 
  11. $post = get_post( $id, ARRAY_A ); 
  12.  
  13. if ( 'attachment' != $post['post_type'] ) 
  14.  
  15. if ( isset( $changes['parent'] ) ) 
  16. $post['post_parent'] = $changes['parent']; 
  17.  
  18. if ( isset( $changes['title'] ) ) 
  19. $post['post_title'] = $changes['title']; 
  20.  
  21. if ( isset( $changes['caption'] ) ) 
  22. $post['post_excerpt'] = $changes['caption']; 
  23.  
  24. if ( isset( $changes['description'] ) ) 
  25. $post['post_content'] = $changes['description']; 
  26.  
  27. if ( MEDIA_TRASH && isset( $changes['status'] ) ) 
  28. $post['post_status'] = $changes['status']; 
  29.  
  30. if ( isset( $changes['alt'] ) ) { 
  31. $alt = wp_unslash( $changes['alt'] ); 
  32. if ( $alt != get_post_meta( $id, '_wp_attachment_image_alt', true ) ) { 
  33. $alt = wp_strip_all_tags( $alt, true ); 
  34. update_post_meta( $id, '_wp_attachment_image_alt', wp_slash( $alt ) ); 
  35.  
  36. if ( wp_attachment_is( 'audio', $post['ID'] ) ) { 
  37. $changed = false; 
  38. $id3data = wp_get_attachment_metadata( $post['ID'] ); 
  39. if ( ! is_array( $id3data ) ) { 
  40. $changed = true; 
  41. $id3data = array(); 
  42. foreach ( wp_get_attachment_id3_keys( (object) $post, 'edit' ) as $key => $label ) { 
  43. if ( isset( $changes[ $key ] ) ) { 
  44. $changed = true; 
  45. $id3data[ $key ] = sanitize_text_field( wp_unslash( $changes[ $key ] ) ); 
  46.  
  47. if ( $changed ) { 
  48. wp_update_attachment_metadata( $id, $id3data ); 
  49.  
  50. if ( MEDIA_TRASH && isset( $changes['status'] ) && 'trash' === $changes['status'] ) { 
  51. wp_delete_post( $id ); 
  52. } else { 
  53. wp_update_post( $post ); 
  54.