Skip Navigation

[Resolved] How to automatically set a date field to the current date/time

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

Problem:
A Toolset Form is used to publish posts. A custom date field should store the time the form was submitted.

Solution:
You would use the cred_save_data hook to trigger code that sets a custom field value with the current timestamp using update_post_meta.

An example (which updates two custom fields) is given below: https://toolset.com/forums/topic/automatic-filling-date-fields/#post-1251117

Note that a simpler implementation would be to just use the standard post_date field of the post published with the form.

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

100% of people find this useful.

This support ticket is created 5 years, 6 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 36 replies, has 3 voices.

Last updated by stephaneB-5 5 years, 6 months ago.

Assisted by: Nigel.

Author
Posts
#1251145

and it will be executed automatically ?

#1251497

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Stephane

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.

There's not much more I can do.

#1251517

Hi Nigel,
a last clarification, the array array(123) array, I replace 123 by the id of my form, is that right?

Thank you for all these clarifications.

#1251521

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

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 );
    }
}
#1251541

Thank you very much.
I have one last question,
I have a checkbox to check, is it possible to do it automatically or not?

#1251551

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2019-05-28 at 09.09.48.png

You mean when the form loads the checkbox should already be checked?

You can provide a default value for the checkbox field (which should be the value of the field when checked).

If you expand the field when editing the form you should see where you can provide the default value (e.g. see screenshot).

#1251581

thank you Nigel !
You're the best 😉

#1251585

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You're welcome. 🙂

Let's close here then.

#1251871

Excuse but where I see the id of the form ? it's the slug ?

#1251903

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2019-05-28 at 14.56.21.png

You can see them on the list of forms at Toolset > Post Forms.

#1251925

oh thank you, I hadn't thought of that.
On the other hand, the time is not right for the right time zone. (I'd like the time zone of paris)

#1251979
Capture d’écran 2019-05-28 à 16.48.53.png

and how it's possible to check automatically this checkbox on validate the form ?

#1252795

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

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.

#1255153

Hi Nigel,

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?

#1255175

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Can you share the code you are actually using?

And what is the PHP error?