Skip Navigation

[Resolved] Using cred-save_data hook to populate date field on form submission

This thread is resolved. Here is a description of the problem and solution.

Problem:
I have custom field “wpcf-start-date” and “wpcf-end-date”.

I'm trying to get the end date to be the same as the start date if the user submits the form leaving the end date blank.

Solution:
Please add this code in your theme’s or child theme’s functions.php file:

function save_data_form_48($post_id, $form_data){
  // Change your CRED Form "ID" accordingly below
  if ($form_data['id']==48){
    
   if(isset($_POST['wpcf-end-date']['display-only']) && $_POST['wpcf-end-date']['display-only'] == ''){ // End date is empty
    $custom_date = get_post_meta( $post_id, 'wpcf-start-date', true );
    update_post_meta($post_id, 'wpcf-end-date', $custom_date);
   }
  }
 }
add_action('cred_save_data', 'save_data_form_48',10,2);

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Tagged: 

This topic contains 4 replies, has 2 voices.

Last updated by julieP 7 years, 3 months ago.

Assisted by: Noman.

Author
Posts
#564591

Hi

I have custom field wpcf-start-date and wpcf-end-date.

I'm trying to get the end date to be the same as the start date if the user submits the form leaving the end date blank.

I've tried this hook but it's not working:-

function save_data_form_48($post_id, $form_data){
            // Change your CRED Form "ID" accordingly below
            if ($form_data['id']==48){
                
                //'wpcf-start-date' is the field value we want to use as the value for 'wpcf-end-date'
                $custom_date = get_post_meta( $post_id, 'wpcf-start-date', true );
                
                    $my_post = array(
                    'ID' => $post_id,
                    'end_date' => $custom_date,                                        
                );
                
                // Update the post into the database
                wp_update_post( $my_post );
                
            }
        }
    add_action('cred_save_data', 'save_data_form_48',10,2);

What am I doing wrong please?

Thanks

#564606

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Julie,

Thank you for contacting Toolset support. Please add this code in your theme’s or child theme’s functions.php file:

function save_data_form_48($post_id, $form_data){
  // Change your CRED Form "ID" accordingly below
  if ($form_data['id']==48){
   
   if(isset($_POST['wpcf-end-date']['display-only']) && $_POST['wpcf-end-date']['display-only'] == ''){ // End date is empty
    $custom_date = get_post_meta( $post_id, 'wpcf-start-date', true );
    update_post_meta($post_id, 'wpcf-end-date', $custom_date);
   }
  }
 }
add_action('cred_save_data', 'save_data_form_48',10,2);

Thank you

#564657

Hi Noman

Thank you for this. It looked very promising but I'm afraid it's not working. I've double checked the form ID and the custom field slugs & they're all correct.

Any ideas please?

#564688

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

To further debug this issue I need to take a look at your setup and to request temporary website WP-admin and FTP info for your site to debug further and to help in resolving this. Your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

✙ Please add the Page link, CPT link and CRED form backend Edit Screen.

Thank you

#564723

Hi again

This is a development site so I tidied a few things up ready to give you access and when I re-tested to make sure the same thing was happening, it worked!!

So my apologies, your code was good; I clearly had something wrong.

Many thanks for sorting this for me.