wp_ajax_add_meta

Ajax handler for adding meta.

Description

wp_ajax_add_meta(); 

Usage

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

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_add_meta() { 
  2. check_ajax_referer( 'add-meta', '_ajax_nonce-add-meta' ); 
  3. $c = 0; 
  4. $pid = (int) $_POST['post_id']; 
  5. $post = get_post( $pid ); 
  6.  
  7. if ( isset($_POST['metakeyselect']) || isset($_POST['metakeyinput']) ) { 
  8. if ( !current_user_can( 'edit_post', $pid ) ) 
  9. wp_die( -1 ); 
  10. if ( isset($_POST['metakeyselect']) && '#NONE#' == $_POST['metakeyselect'] && empty($_POST['metakeyinput']) ) 
  11. wp_die( 1 ); 
  12.  
  13. // If the post is an autodraft, save the post as a draft and then attempt to save the meta. 
  14. if ( $post->post_status == 'auto-draft' ) { 
  15. $post_data = array(); 
  16. $post_data['action'] = 'draft'; // Warning fix 
  17. $post_data['post_ID'] = $pid; 
  18. $post_data['post_type'] = $post->post_type; 
  19. $post_data['post_status'] = 'draft'; 
  20. $now = current_time('timestamp', 1); 
  21. /** translators: 1: Post creation date, 2: Post creation time */ 
  22. $post_data['post_title'] = sprintf( __( 'Draft created on %1$s at %2$s' ), date( __( 'F j, Y' ), $now ), date( __( 'g:i a' ), $now ) ); 
  23.  
  24. $pid = edit_post( $post_data ); 
  25. if ( $pid ) { 
  26. if ( is_wp_error( $pid ) ) { 
  27. $x = new WP_Ajax_Response( array( 
  28. 'what' => 'meta',  
  29. 'data' => $pid 
  30. ) ); 
  31. $x->send(); 
  32.  
  33. if ( !$mid = add_meta( $pid ) ) 
  34. wp_die( __( 'Please provide a custom field value.' ) ); 
  35. } else { 
  36. wp_die( 0 ); 
  37. } elseif ( ! $mid = add_meta( $pid ) ) { 
  38. wp_die( __( 'Please provide a custom field value.' ) ); 
  39.  
  40. $meta = get_metadata_by_mid( 'post', $mid ); 
  41. $pid = (int) $meta->post_id; 
  42. $meta = get_object_vars( $meta ); 
  43. $x = new WP_Ajax_Response( array( 
  44. 'what' => 'meta',  
  45. 'id' => $mid,  
  46. 'data' => _list_meta_row( $meta, $c ),  
  47. 'position' => 1,  
  48. 'supplemental' => array('postid' => $pid) 
  49. ) ); 
  50. } else { // Update? 
  51. $mid = (int) key( $_POST['meta'] ); 
  52. $key = wp_unslash( $_POST['meta'][$mid]['key'] ); 
  53. $value = wp_unslash( $_POST['meta'][$mid]['value'] ); 
  54. if ( '' == trim($key) ) 
  55. wp_die( __( 'Please provide a custom field name.' ) ); 
  56. if ( '' == trim($value) ) 
  57. wp_die( __( 'Please provide a custom field value.' ) ); 
  58. if ( ! $meta = get_metadata_by_mid( 'post', $mid ) ) 
  59. wp_die( 0 ); // if meta doesn't exist 
  60. if ( is_protected_meta( $meta->meta_key, 'post' ) || is_protected_meta( $key, 'post' ) || 
  61. ! current_user_can( 'edit_post_meta', $meta->post_id, $meta->meta_key ) || 
  62. ! current_user_can( 'edit_post_meta', $meta->post_id, $key ) ) 
  63. wp_die( -1 ); 
  64. if ( $meta->meta_value != $value || $meta->meta_key != $key ) { 
  65. if ( !$u = update_metadata_by_mid( 'post', $mid, $value, $key ) ) 
  66. wp_die( 0 ); // We know meta exists; we also know it's unchanged (or DB error, in which case there are bigger problems). 
  67.  
  68. $x = new WP_Ajax_Response( array( 
  69. 'what' => 'meta',  
  70. 'id' => $mid, 'old_id' => $mid,  
  71. 'data' => _list_meta_row( array( 
  72. 'meta_key' => $key,  
  73. 'meta_value' => $value,  
  74. 'meta_id' => $mid 
  75. ), $c ),  
  76. 'position' => 0,  
  77. 'supplemental' => array('postid' => $meta->post_id) 
  78. ) ); 
  79. $x->send();