Problem:
How to check if a field value has changed when submitting a CRED that edits a Post
And if changed, update/add some actions.
Solution:
1. Use the cred_before_save_data action, as otherwise the past is already updated when you compare.
2, This is a example code, fully commented:
function record_status_change($form_data) { // if a specific form if ($form_data['id']==168) //CRED Form ID { //get existing post id $existing_post_id = $form_data['container_id']; //Current CONTAINER of Form (current Post) //https://toolset.com/documentation/user-guides/cred-api/#cbsd //get existing post field value $existing_post_status = get_post_meta($existing_post_id, 'wpcf-value', true); //Get post meta value of currnet post //get new field value $new_field_value = $_POST['wpcf-value']; //get value of current CRED Form field //If value has changed if ($existing_post_status != $new_field_value) //if this is not the same { //if, then insert the new post //values please refer to this DOC: //https://codex.wordpress.org/Function_Reference/wp_insert_post //I already put in a few examples, it's to adapt. Please read the WP Codex carefully for debug. $new_post = array( 'post_content' => '', 'post_excerpt' => '', 'post_name' => 'title', 'post_status' => 'publish', 'post_title' => 'title', 'post_type' => 'post', 'menu_order' => ''); //insert post $new_post_id = wp_insert_post( $new_post ); //NOW Update the new posts fields! required. //(it shoudl automatically create them if non-existent) update_post_meta($new_post_id, 'your-new-custom-field-slug', 1); } } } add_action('cred_before_save_data', 'record_status_change',10,1);
3. This checks the FORM's fields value against the CURRENT posts (same field) value.
The FORM must be on the POST that is edited currently
Relevant Documentation:
https://codex.wordpress.org/Function_Reference/wp_insert_post
https://codex.wordpress.org/Function_Reference/update_post_meta
https://toolset.com/documentation/user-guides/cred-api/#cbsd
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | - | 14:00 – 20:00 | 14:00 – 20:00 | 14:00 – 20:00 | 14:00 – 20:00 | 14:00 – 20:00 |
- | - | - | - | - | - | - |
Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)
This topic contains 13 replies, has 2 voices.
Last updated by gavinS 8 years, 3 months ago.
Assisted by: Beda.