wpseo_upsert_meta

Helper function to update a post's meta data, returning relevant information about the information updated and the results or the meta update.

Description

(string) wpseo_upsert_meta( (int) $post_id, (string) $new_meta_value, (string) $orig_meta_value, (string) $meta_key, (string) $return_key ); 

Returns (string)

Parameters (5)

0. $post_id (int)
The post id.
1. $new_meta_value (string)
New meta value to record.
2. $orig_meta_value (string)
The orig meta value.
3. $meta_key (string)
Meta key string.
4. $return_key (string)
Return key string to use in results.

Usage

  1. if ( !function_exists( 'wpseo_upsert_meta' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'yoast-seo/admin/ajax.php'; 
  3.  
  4. // The post id. 
  5. $post_id = -1; 
  6.  
  7. // New meta value to record. 
  8. $new_meta_value = ''; 
  9.  
  10. // The orig meta value. 
  11. $orig_meta_value = ''; 
  12.  
  13. // Meta key string. 
  14. $meta_key = ''; 
  15.  
  16. // Return key string to use in results. 
  17. $return_key = ''; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = wpseo_upsert_meta($post_id, $new_meta_value, $orig_meta_value, $meta_key, $return_key); 
  21.  

Defined (1)

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

/admin/ajax.php  
  1. function wpseo_upsert_meta( $post_id, $new_meta_value, $orig_meta_value, $meta_key, $return_key ) { 
  2.  
  3. $post_id = intval( $post_id ); 
  4. $sanitized_new_meta_value = wp_strip_all_tags( $new_meta_value ); 
  5. $orig_meta_value = wp_strip_all_tags( $orig_meta_value ); 
  6.  
  7. $upsert_results = array( 
  8. 'status' => 'success',  
  9. 'post_id' => $post_id,  
  10. "new_{$return_key}" => $sanitized_new_meta_value,  
  11. "original_{$return_key}" => $orig_meta_value,  
  12. ); 
  13.  
  14. $the_post = get_post( $post_id ); 
  15. if ( empty( $the_post ) ) { 
  16.  
  17. $upsert_results['status'] = 'failure'; 
  18. $upsert_results['results'] = __( 'Post doesn\'t exist.', 'wordpress-seo' ); 
  19.  
  20. return $upsert_results; 
  21.  
  22. $post_type_object = get_post_type_object( $the_post->post_type ); 
  23. if ( ! $post_type_object ) { 
  24.  
  25. $upsert_results['status'] = 'failure'; 
  26. $upsert_results['results'] = sprintf( __( 'Post has an invalid Post Type: %s.', 'wordpress-seo' ), $the_post->post_type ); 
  27.  
  28. return $upsert_results; 
  29.  
  30. if ( ! current_user_can( $post_type_object->cap->edit_posts ) ) { 
  31.  
  32. $upsert_results['status'] = 'failure'; 
  33. $upsert_results['results'] = sprintf( __( 'You can\'t edit %s.', 'wordpress-seo' ), $post_type_object->label ); 
  34.  
  35. return $upsert_results; 
  36.  
  37. if ( ! current_user_can( $post_type_object->cap->edit_others_posts ) && $the_post->post_author != get_current_user_id() ) { 
  38.  
  39. $upsert_results['status'] = 'failure'; 
  40. $upsert_results['results'] = sprintf( __( 'You can\'t edit %s that aren\'t yours.', 'wordpress-seo' ), $post_type_object->label ); 
  41.  
  42. return $upsert_results; 
  43.  
  44.  
  45. if ( $sanitized_new_meta_value === $orig_meta_value && $sanitized_new_meta_value !== $new_meta_value ) { 
  46. $upsert_results['status'] = 'failure'; 
  47. $upsert_results['results'] = __( 'You have used HTML in your value which is not allowed.', 'wordpress-seo' ); 
  48.  
  49. return $upsert_results; 
  50.  
  51. $res = update_post_meta( $post_id, $meta_key, $sanitized_new_meta_value ); 
  52.  
  53. $upsert_results['status'] = ( $res !== false ) ? 'success' : 'failure'; 
  54. $upsert_results['results'] = $res; 
  55.  
  56. return $upsert_results;