I've updated the code example to demonstrate how you could modify it so that you have two forms, and when one is submitted the current time is saved as a custom field you specify, and when the other form is submitted the current time is saved as a different custom field you specify.
This code runs using the cred_save_data hook, meaning that it runs each time a form is submitted, and the custom fields are updated for the post the the form submits.
Sorry! I didn't paste the updated code example, here it is.
Yes, 123 and in this updated example 789 are the IDs of the forms.
/**
* Automatically set date fields on form submission
*/
add_action('cred_save_data', 'tssupp_form_submit', 10, 2);
function tssupp_form_submit($post_id, $form_data)
{
// Edit
$start_form_id = 123; // ID of form with start time
$end_form_id = 789; // ID of form with end time
$start = 'entry'; // slug of start field
$end = 'exit'; // slug of end field
$now = time(); // Current time and date as a timestamp
if ( $form_data['id'] == $start_form_id ) {
update_post_meta( $post_id, 'wpcf-'.$start, $now );
} elseif ( $form_data['id'] == $end_form_id ) {
update_post_meta( $post_id, 'wpcf-'.$end, $now );
}
}
I updated the sample code to change how the date is stored because of an oddity in how Types stores dates, and the code is below.
It assumes that in Settings > General on your site where the timezone is specified you set it to the location (i.e. Europe > Paris) rather than a UTC offset, which would require a slightly different solution.
/**
* Automatically set date fields on form submission
*/
add_action('cred_save_data', 'tssupp_form_submit', 10, 2);
function tssupp_form_submit($post_id, $form_data)
{
// Edit
$start_form_id = 123; // ID of form with start time
$end_form_id = 789; // ID of form with end time
$start = 'entry'; // slug of start field
$end = 'exit'; // slug of end field
// Types date field expects date to be stored as "local" timestamp
$site_timezone = get_option('timezone_string');
$timezone_offset_in_seconds = timezone_offset_get(timezone_open($site_timezone), new DateTime());
$now = time() + $timezone_offset_in_seconds;
if ($form_data['id'] == $start_form_id) {
update_post_meta($post_id, 'wpcf-' . $start, $now);
} elseif ($form_data['id'] == $end_form_id) {
update_post_meta($post_id, 'wpcf-' . $end, $now);
}
}
Regarding the checkbox, it seems like you cannot provide a default value in the drag and drop editor, so you will need to switch to expert mode and locate the shortcode that inserts this checkbox field and then add a value attribute to the shortcode. If your checkbox saves 1 when checked then you would add a value="1" attribute.
when I enter the code and replace timezone_string with Europe/Paris, I have a php error and I don't see why unfortunately.
Is it my replacement that is not good?