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?
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
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.
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
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.
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?
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"]');
My issue is resolved now. Thank you!