otw_save_postdata

When the post is saved, saves our custom data.

Description

otw_save_postdata( $post_id ); 

Parameters (1)

0. $post_id
The post id.

Usage

  1. if ( !function_exists( 'otw_save_postdata' ) ) { 
  2. require_once '/functions.php'; 
  3.  
  4. // The post id. 
  5. $post_id = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = otw_save_postdata($post_id); 
  9.  

Defined (1)

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

/functions.php  
  1. function otw_save_postdata( $post_id ) { 
  2.  
  3. // First we need to check if the current user is authorised to do this action. 
  4. if ( isset($_REQUEST['post_type']) && $_REQUEST['post_type'] != 'page') { 
  5. if ( ! current_user_can( 'edit_page', $post_id ) ) 
  6. return; 
  7. } else { 
  8. if ( ! current_user_can( 'edit_post', $post_id ) ) 
  9. return; 
  10.  
  11. global $post; 
  12. if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 
  13. return; 
  14. }; 
  15.  
  16. // Secondly we need to check if the user intended to change this value. 
  17. if ( ! isset( $_POST['otw_noncename'] ) || ! wp_verify_nonce( $_POST['otw_noncename'], plugin_basename( __FILE__ ) ) ) 
  18. return; 
  19.  
  20. update_post_meta($post->ID, "otw_head_title_setting", $_POST["otw_head_title_setting"]); 
  21.  
  22. $post_ID = $_POST['post_ID']; 
  23. //sanitize user input 
  24. $mydata = sanitize_text_field( $_POST['otw_page_sub_title'] ); 
  25.  
  26. add_post_meta($post_ID, '_otw_meta_value_key', $mydata, true) or 
  27. update_post_meta($post_ID, '_otw_meta_value_key', $mydata); 
  28.