1

I'm try to check input data before it is added to DB. For example:

I have a custom post meta named book_isbn in post page. I want to make sure that this variable $_POST['book_isbn'] is valid before it is added to DB.

So far I know that I can use hook wp_insert_post_data to achieve this. I'm not sure if I did this right. It doesn't work for me. Please check the code below.

add_filter( 'wp_insert_post_data', 'clean_data_before_post_created', '99', 2 );

    function clean_data_before_ad_created( $data , $postarr ) {
          $data['book_isbn'] = clean_isbn($_POST['book_isbn']);
          return $data;
     }

Thank you.

0

This runs your function right after the post has been saved (created/updated):

add_action('save_post','clean_data_while_saving_post',1);

function clean_data_while_saving_post($post_id) {
  $data = get_post_meta($post_id,'book_isbn',true);
  update_post_meta($post_id,'book_isbn',clean_isbn($data));
}
  • Thank you but this doesn't work either. And your filter missing the last parameter. – Louis Tran Feb 16 '16 at 18:48
  • Does this really have to happen before the post is created, or could this also be done right after (while) the post is created? – Johano Fierra Feb 16 '16 at 20:26
  • This must be happened before the post is inserted into database. – Louis Tran Feb 16 '16 at 20:55
  • May I ask how the custom post meta book_isbn is added to the post? Is it done via the "Custom Field" box, or via a plugin/theme option? Also, I changed my answer, would that work for you? – Johano Fierra Feb 16 '16 at 22:24
  • This doesn't work because at this point, there is no saved data in DB so you can't call get_post_meta to retrieve the meta_value. – Louis Tran Feb 16 '16 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.