Skip Navigation

[Resolved] Saving modified by information with each field in the edit form

This support ticket is created 4 years, 2 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/Karachi (GMT+05:00)

This topic contains 7 replies, has 2 voices.

Last updated by nabils 4 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1770211

Hi,
I have a post form to edit a post with different select fields. Also, I have different single line fields to save modified by name. I would like to save in modified by field the select field was modified by whom. For example if select field "a" was modified, I want to same in single line "a " who modified it. How this can be done?

#1771627

Hi,

Thank you for waiting.

During testing on my website, I was able to achieve this using the "cred_before_save_data" action.
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data )

For this example, suppose that a form has two radio fields:

1. wpcf-book-radio-1
2. wpcf-book-radio-2

And respectively, there are two single-line fields, to store the ID of the user who modified the value, last time.

1. wpcf-book-radio-1-hidden
2. wpcf-book-radio-2-hidden

If the edit form's ID is "123", the code for the function would look like this:


add_action('cred_before_save_data', 'my_before_save_data_action',10,1);
function my_before_save_data_action($form_data)
{
	// if a specific form
	if ($form_data['id']==123)
	{
		$post_id = $_POST['_cred_cred_prefix_post_id'];
		$user_id = do_shortcode('[wpv-current-user info="id"]');
		$post_meta = get_post_meta( $post_id );

		// field "wpcf-book-radio-1"
		if($post_meta['wpcf-book-radio-1'][0] != $_POST['wpcf-book-radio-1']){
			$_POST['wpcf-book-radio-1-hidden'] = $user_id;
		}

		// field "wpcf-book-radio-2"
		if($post_meta['wpcf-book-radio-2'][0] != $_POST['wpcf-book-radio-2']){
			$_POST['wpcf-book-radio-2-hidden'] = $user_id;
		}
	}
}

Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1774693

Thank you for your reply. I have modified the code and added to the functions.php. However, when a user is modifying one filed the user name will be stored as they modify all fields. Please advise.

#1776693

Hi,

Thanks for the update.

Using the access details that you shared in the chat, I'm not able to access the WordPress admin area.

Can you please share the access details of two different users who can modify the fields in this form? Please also share the exact code snippet that you've used for this.

Note: I've set your next reply as private.

regards,
Waqar

#1778277

Thank you for sharing the admin access.

I noticed that in the hidden fields, a default value is being set using the "[wpv-current-user]" shortcode.
( screenshot: hidden link )

We need the originally saved value from the database in these fields so that our custom function can perform the comparison and save the current user's ID if a particular field is modified.

Please remove that "[wpv-current-user]" shortcode from those hidden fields and only use: value=''

Important note: If you still feel that the hidden fields are not working properly, please include full steps and information about the users, that you used for the testing.

#1780489

Thank you, now the user id will be saved only in the modified field. However, how can I save the user login instead of the user id?

#1783371

Thanks for the update and glad that it is working now.

To make the hidden field store the current user's login/username, instead of the ID, you can replace line# 9, from:


$user_id = do_shortcode('[wpv-current-user info="id"]');

To:


$user_id = do_shortcode('[wpv-current-user info="login"]');

#1783927

My issue is resolved now. Thank you!