Skip Navigation

[Resolved] Update custom field value with the current timestamp programmatically.

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

Problem:
Update custom field value with the current timestamp programmatically.

Solution:
You can use the Toolset form's hook "cred_save_data" in order to update the custom field value on fly or programmatically.

You can find the proposed solution in this case with the following reply:
- https://toolset.com/forums/topic/saving-the-datestamp-of-when-an-update-is-done/#post-2365803

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

This support ticket is created 2 years, 7 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 4 replies, has 2 voices.

Last updated by pdC 2 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2365493

pdC

I have a Toolset > Custom Fields Group > that has a date field with the slug name = last-profile-update-on. That field shows up when I edit a user's profile in WP and behaves as expected.

I have a Toolset > User Forms > Update My Profile form that the user uses on the public side to update his profile. I want to show the user a text version of when he did his last update and then save the current datestamp of when he saves his updates into the last-profile-update-on field so that it shows up in his profile in WP.

Any help would be appreciated.

#2365635

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To display the date field, what if you try to use the following shortcode:

[types usermeta="your-date-field-slug" user_current="true"][/types]

To update the field, is the date field is available with your edit profile form, if yes, then there is no need to do anything as it will update the field value as the field is present within the update profile form.

#2365797

pdC

That's the statement I use to show the user the last date that he made an update. It's displaying the date of the last update that is already in the WP User record. This is perfect.

How do I set up a hidden field on the front side form that will store the current date into the last-profile-update-on field in his WP profile when he saves his updates ?

Thanks...

#2365803

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

There is no need to add the hidden field. You can use the Toolset form's hook "cred_save_data" and you can use this hook in order to update the "last-profile-update-on" user field value to current time.

Could you please try to add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_action('cred_save_data', 'func_update_user_fields',10,2);
function func_update_user_fields($post_id, $form_data) {


if ($form_data['id']==9999) {

$current_user = wp_get_current_user();
$user_id = $current_user -> ID;

update_user_meta ($user_id, 'wpcf-last-profile-update-on', time());

}
}

Where:
- Replace 9999 with your original user form ID.

Once you add the code to new snippet and save it and activate the code snippet, you should try to update the profile and check if the field gets updated automatically or not.

#2367583

pdC

My issue is resolved now. The date gets stored in the user's profile as desired - perfect!

Thank you!