edit_post

Update an existing post with values provided in $_POST.

Description

(int) edit_post( (null) $post_data = null ); 

Returns (int)

Post ID.

Parameters (1)

0. $post_data — Optional. (null) => null
The post data.

Usage

  1. if ( !function_exists( 'edit_post' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/post.php'; 
  3.  
  4. // The post data. 
  5. $post_data = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = edit_post($post_data); 
  9.  

Defined (1)

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

/wp-admin/includes/post.php  
  1. function edit_post( $post_data = null ) { 
  2. global $wpdb; 
  3.  
  4. if ( empty($post_data) ) 
  5. $post_data = &$_POST; 
  6.  
  7. // Clear out any data in internal vars. 
  8. unset( $post_data['filter'] ); 
  9.  
  10. $post_ID = (int) $post_data['post_ID']; 
  11. $post = get_post( $post_ID ); 
  12. $post_data['post_type'] = $post->post_type; 
  13. $post_data['post_mime_type'] = $post->post_mime_type; 
  14.  
  15. if ( ! empty( $post_data['post_status'] ) ) { 
  16. $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); 
  17.  
  18. if ( 'inherit' == $post_data['post_status'] ) { 
  19. unset( $post_data['post_status'] ); 
  20.  
  21. $ptype = get_post_type_object($post_data['post_type']); 
  22. if ( !current_user_can( 'edit_post', $post_ID ) ) { 
  23. if ( 'page' == $post_data['post_type'] ) 
  24. wp_die( __('Sorry, you are not allowed to edit this page.' )); 
  25. else 
  26. wp_die( __('Sorry, you are not allowed to edit this post.' )); 
  27.  
  28. if ( post_type_supports( $ptype->name, 'revisions' ) ) { 
  29. $revisions = wp_get_post_revisions( $post_ID, array( 'order' => 'ASC', 'posts_per_page' => 1 ) ); 
  30. $revision = current( $revisions ); 
  31.  
  32. // Check if the revisions have been upgraded 
  33. if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) 
  34.  
  35. if ( isset($post_data['visibility']) ) { 
  36. switch ( $post_data['visibility'] ) { 
  37. case 'public' : 
  38. $post_data['post_password'] = ''; 
  39. break; 
  40. case 'password' : 
  41. unset( $post_data['sticky'] ); 
  42. break; 
  43. case 'private' : 
  44. $post_data['post_status'] = 'private'; 
  45. $post_data['post_password'] = ''; 
  46. unset( $post_data['sticky'] ); 
  47. break; 
  48.  
  49. $post_data = _wp_translate_postdata( true, $post_data ); 
  50. if ( is_wp_error($post_data) ) 
  51. wp_die( $post_data->get_error_message() ); 
  52.  
  53. // Post Formats 
  54. if ( isset( $post_data['post_format'] ) ) 
  55. set_post_format( $post_ID, $post_data['post_format'] ); 
  56.  
  57. $format_meta_urls = array( 'url', 'link_url', 'quote_source_url' ); 
  58. foreach ( $format_meta_urls as $format_meta_url ) { 
  59. $keyed = '_format_' . $format_meta_url; 
  60. if ( isset( $post_data[ $keyed ] ) ) 
  61. update_post_meta( $post_ID, $keyed, wp_slash( esc_url_raw( wp_unslash( $post_data[ $keyed ] ) ) ) ); 
  62.  
  63. $format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' ); 
  64.  
  65. foreach ( $format_keys as $key ) { 
  66. $keyed = '_format_' . $key; 
  67. if ( isset( $post_data[ $keyed ] ) ) { 
  68. if ( current_user_can( 'unfiltered_html' ) ) 
  69. update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] ); 
  70. else 
  71. update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) ); 
  72.  
  73. if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) { 
  74. $id3data = wp_get_attachment_metadata( $post_ID ); 
  75. if ( ! is_array( $id3data ) ) { 
  76. $id3data = array(); 
  77.  
  78. foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) { 
  79. if ( isset( $post_data[ 'id3_' . $key ] ) ) { 
  80. $id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) ); 
  81. wp_update_attachment_metadata( $post_ID, $id3data ); 
  82.  
  83. // Meta Stuff 
  84. if ( isset($post_data['meta']) && $post_data['meta'] ) { 
  85. foreach ( $post_data['meta'] as $key => $value ) { 
  86. if ( !$meta = get_post_meta_by_id( $key ) ) 
  87. continue; 
  88. if ( $meta->post_id != $post_ID ) 
  89. continue; 
  90. if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) ) 
  91. continue; 
  92. update_meta( $key, $value['key'], $value['value'] ); 
  93.  
  94. if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) { 
  95. foreach ( $post_data['deletemeta'] as $key => $value ) { 
  96. if ( !$meta = get_post_meta_by_id( $key ) ) 
  97. continue; 
  98. if ( $meta->post_id != $post_ID ) 
  99. continue; 
  100. if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) ) 
  101. continue; 
  102. delete_meta( $key ); 
  103.  
  104. // Attachment stuff 
  105. if ( 'attachment' == $post_data['post_type'] ) { 
  106. if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) { 
  107. $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] ); 
  108. if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) { 
  109. $image_alt = wp_strip_all_tags( $image_alt, true ); 
  110. // update_meta expects slashed. 
  111. update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); 
  112.  
  113. $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array(); 
  114.  
  115. /** This filter is documented in wp-admin/includes/media.php */ 
  116. $post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data ); 
  117.  
  118. // Convert taxonomy input to term IDs, to avoid ambiguity. 
  119. if ( isset( $post_data['tax_input'] ) ) { 
  120. foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) { 
  121. // Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary. 
  122. if ( is_taxonomy_hierarchical( $taxonomy ) ) { 
  123. continue; 
  124.  
  125. /** 
  126. * Assume that a 'tax_input' string is a comma-separated list of term names. 
  127. * Some languages may use a character other than a comma as a delimiter, so we standardize on 
  128. * commas before parsing the list. 
  129. */ 
  130. if ( ! is_array( $terms ) ) { 
  131. $comma = _x( ', ', 'tag delimiter' ); 
  132. if ( ', ' !== $comma ) { 
  133. $terms = str_replace( $comma, ', ', $terms ); 
  134. $terms = explode( ', ', trim( $terms, " \n\t\r\0\x0B, " ) ); 
  135.  
  136. $clean_terms = array(); 
  137. foreach ( $terms as $term ) { 
  138. // Empty terms are invalid input. 
  139. if ( empty( $term ) ) { 
  140. continue; 
  141.  
  142. $_term = get_terms( $taxonomy, array( 
  143. 'name' => $term,  
  144. 'fields' => 'ids',  
  145. 'hide_empty' => false,  
  146. ) ); 
  147.  
  148. if ( ! empty( $_term ) ) { 
  149. $clean_terms[] = intval( $_term[0] ); 
  150. } else { 
  151. // No existing term was found, so pass the string. A new term will be created. 
  152. $clean_terms[] = $term; 
  153.  
  154. $post_data['tax_input'][ $taxonomy ] = $clean_terms; 
  155.  
  156. add_meta( $post_ID ); 
  157.  
  158. update_post_meta( $post_ID, '_edit_last', get_current_user_id() ); 
  159.  
  160. $success = wp_update_post( $post_data ); 
  161. // If the save failed, see if we can sanity check the main fields and try again 
  162. if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) { 
  163. $fields = array( 'post_title', 'post_content', 'post_excerpt' ); 
  164.  
  165. foreach ( $fields as $field ) { 
  166. if ( isset( $post_data[ $field ] ) ) { 
  167. $post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] ); 
  168.  
  169. wp_update_post( $post_data ); 
  170.  
  171. // Now that we have an ID we can fix any attachment anchor hrefs 
  172. _fix_attachment_links( $post_ID ); 
  173.  
  174. wp_set_post_lock( $post_ID ); 
  175.  
  176. if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) { 
  177. if ( ! empty( $post_data['sticky'] ) ) 
  178. stick_post( $post_ID ); 
  179. else 
  180. unstick_post( $post_ID ); 
  181.  
  182. return $post_ID;