Skip Navigation

[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:

https://toolset.com/forums/topic/need-separate-timestamp-for-last-update-from-backend-and-from-cred-form/#post-1167460

Relevant Documentation:

This support ticket is created 5 years, 11 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 7 replies, has 3 voices.

Last updated by Luo Yang 5 years, 11 months ago.

Assisted by: Luo Yang.

Author
Posts
#1165298

Hi -

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?

Thanks,
Jeff

#1165477

Minesh
Supporter

Languages: English (English )

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

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?

#1165822

Hi, Minesh -

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

Thanks,
Jeff

#1166251

Minesh
Supporter

Languages: English (English )

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

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".

#1166445

Hi Minesh -

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.

Thanks for your effort,
Jeff

#1167460

Dear Jeff,

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;
	}
#1169396

My issue is resolved now. Thank you!

#1170066

You are welcome