[Resolved] Need separate timestamp for last update from backend and from CRED form
This thread is resolved. Here is a description of the problem and solution.
Problem:
I have an existing timestamp for when the CPT member-profile is updated by Admin. That works fine. Now I want a separate timestamp to keep track of when the member updates his own profile from the front end via CRED form. I got that to work using cred_save_data hook. The problem is, my backend hook is using save_post hook, so when member saves his profile on the front end, it updates both fields.
Is there a way I can check this from the save_post hook?
Solution:
There is a hidden field "_cred_cred_prefix_form_id" in Toolset post form, it stores value of current Toolset form ID, you can use it to check if it is submitted by Toolset post form insider in the "save_post" action hook, for example, change this line of your PHP codes from:
I have a membership site all done from scratch with Types/Views. It does not use the WordPress Profile (except for login.)
I have an existing timestamp for when the CPT member-profile is updated by Admin. That works fine. Now I want a separate timestamp to keep track of when the member updates his own profile from the front end via CRED form. I got that to work using cred_save_data hook. The problem is, my backend hook is using save_post hook, so when member saves his profile on the front end, it updates both fields.
Is there a way I can check this from the save_post hook? Or some other way?
Hello. Thank you for contacting the Toolset support.
I have an existing timestamp for when the CPT member-profile is updated by Admin. That works fine.
==> OK
Now I want a separate timestamp to keep track of when the member updates his own profile from the front end via CRED form. I got that to work using cred_save_data hook.
==> OK
The problem is, my backend hook is using save_post hook, so when member saves his profile on the front end, it updates both fields.
==> I would like to know here when you use "cred_save_data" and "save_post" does it target the same field (timestamp) value?
- Do you have two separate fields to store the timestamp value?
1. I have 2 fields in the Members Profile CPT: wpcf-last-mod-member and wpcf-last-mod-admin.
2. Yes, the timestamps are the same in both fields after submitting the CRED form.
There is likely a few milliseconds difference, but I am not displaying milliseconds so I cannot see that. I understand both of these code hooks are executing as expected, but I am trying to figure out how to only update the wpcf-last-mod-member field. I need to separate these two because when the member logs in, I am displaying to them their last profile modification date. So, I do not want to display the admin's date in case there was an admin-type update.
Sorry, I forgot to include my code in above post.
Code for member's CRED form submit:
// Form: Edit My Profile (Edit Current Profile) - Update timestamp of just modified profile
add_action('cred_save_data', 'update_last_mod_date_by_member',10,2);
function update_last_mod_date_by_member($post_id, $form_data) {
if ($form_data['id'] == 2009) { // is this Edit Current Profile form?
$tz = date_default_timezone_set('America/Los_Angeles');
$new_timestamp = date("F j, Y, g:i A T");
update_post_meta($post_id, 'wpcf-last-modified-by-member', $new_timestamp);
}
}
Code for admin's backend Types submit:
// Profile last modified by Admin - Get current date/time and save in CNSV Member Profiles -> wpcf-last-modified
add_action('save_post', 'cnsv_last_mod_date_timestamp',20,2);
function cnsv_last_mod_date_timestamp($post_id, $post) {
$thePostType = get_post_type($post_id);
if ($thePostType == 'members') {
$tz = date_default_timezone_set('America/Los_Angeles');
$admin_last_mod_current_datetime = date("F j, Y, g:i A T");
update_post_meta($post_id, 'wpcf-last-modified', $admin_last_mod_current_datetime);
}
}
Ok - well - I see that you are having two custom fields so both fields have the place in the database to store its own value. So, its obvious again that when you use "cred_save_data" it will update the "wpcf-last-modified-by-member" field and when its done from admin, it will update the current time for the field "wpcf-last-modified".
I do not see any issues here but it could be that timezone issue or I'm not sure how it updates the "wpcf-last-modified-by-member" field value when you update the post from admin which acctually targets the field "wpcf-last-modified".
I’m sorry you do not seem to understand my problem, and I don’t know how to better explain it. Can you please forward my issue to another support person? I don’t know if that’s possible. If not, I can close this issue and post again.
There is a hidden field "_cred_cred_prefix_form_id" in Toolset post form, it stores value of current Toolset form ID, you can use it to check if it is submitted by Toolset post form insider in the "save_post" action hook, for example, change this line of your PHP codes from:
add_action('save_post', 'cnsv_last_mod_date_timestamp',20,2);
function cnsv_last_mod_date_timestamp($post_id, $post) {
To:
add_action('save_post', 'cnsv_last_mod_date_timestamp',20,2);
function cnsv_last_mod_date_timestamp($post_id, $post) {
if(isset($_POST['_cred_cred_prefix_form_id']) && $_POST['_cred_cred_prefix_form_id'] = 2009){
// it is submitted by Toolset form
return;
}