custom user meta in hidden fields?
-
I would like to add some custom user data when submit a form such like their name, phone etc.. the user meta created by ACF and register works, but I have no idea to send them in another forminator form..
May I know how to solve this?
-
Follow this link, and i try to add the code below in mu-plugins
<?php add_filter('forminator_field_hidden_field_value', function ($value) { if (!is_user_logged_in()) { return $value; } $user = wp_get_current_user(); $user_data = get_userdata($user->ID); // To show all possible values //print_r($user_data); if (strpos($value, 'user_bio') !== false) { $value = $user_data->description; return $value; } elseif (strpos($value, 'user_website') !== false) { $value = $user_data->user_url; return $value; }elseif (strpos($value, 'bank_account_number') !== false) { $value = $user_data->bank_account_number; return $value; } return $value; } , 20, 1);But it didnt show the account number eg: 123123
it show the submission like this, how I fix itDate Submitted Jan 22, 2024 @ 7:14 PM Hidden bank_account_number Name Hong2I hope you’re well today!
You’re on a right track but, unfortunately, this code is a bit outdated. Since then there were multiple updates to the plugin and at some point due to security requirements there were changes made to how hidden fields are handled. There are now additionally validated and even if you change their values in page source (which is, essentially, what this code does), those values will be stripped and only the values originally set in hidden field configuration would be saved.
You’ll need a bit different code to handle that:
<?php add_action('forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_fields_data', 10, 3); function wpmudev_change_hidden_fields_data($entry, $module_id, $field_data_array) { $form_ids = array(3010); //Please change the form ID if (!in_array($module_id, $form_ids)) { return; } if (!is_user_logged_in()) { return; } $user = wp_get_current_user(); $user_data = get_userdata($user->ID); foreach ($field_data_array as $key => $value) { // HERE YOU MODIFY YOUR HIDDEN FIELDS VALUES # set user bio if (strpos($value['value'], 'user_bio') !== false) { Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = $user_data->description; } } }Note: in page source you will not see any changes and actual user data; only in submission.
To “configure” the code
1. in this line
$form_ids = array(3010); //Please change the form IDreplace the 3010 number with your form ID; form ID is the number you see in form’s shortcode
2. this is the block that sets actual value of the field:
# set user bio if (strpos($value['value'], 'user_bio') !== false) { Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = $user_data->description; }It works based on the similar principle as in the code that you already tried so I believe you got the idea. To set other fields, you just need to repeat that block and adjust field name and the data you assign to it.
—-
If you also need those hidden fields to be reflected in e-mail notifications (not only DB saved submissions) you may need to add another piece of code to the same MU plugin file:
add_filter('forminator_prepared_data', 'wpmudev_update_hidden_field_val_copy', 10, 2); function wpmudev_update_hidden_field_val_copy($prepared_data, $module_object) { $form_ids = array(3010); //Please change the form ID if (!in_array($module_object->id, $form_ids)) { return $prepared_data; } foreach ($prepared_data as $key => $value) { // repeat this block as many times as the number of hidden fields that you modified with code // note: here you don't use field values but actual hidden fields IDs // of hidden fields that you modified. if (strpos($key, 'hidden-1') !== false) { $prepared_data[$key] = sanitize_text_field($_POST[$key]); } } return $prepared_data; }Again, in this line
$form_ids = array(3010); //Please change the form IDyou will need to replace number 3010 with the form ID (same as with other code above).
And this block of code
if (strpos($key, 'hidden-1') !== false) { $prepared_data[$key] = sanitize_text_field($_POST[$key]); }will need to be repeated as many times as the number of hidden fields that you modified with the other code. Note that here you – as in above example – you need to use hidden fields IDs; you need to use the same IDs as the IDs of the fields that are modified.
Kind regards,
Adamthe email things is a well consider for me , thanks , what a fantastic team!
will have a try nowone more question, if I want to fileupload a image in the form, can it send the media url in email?
one more question, if I want to fileupload a image in the form, can it send the media url in email?
Yes. When you configure e-mail notification for the form and use one of these macros
{all_fields} {all_non_empty_fields} {upload-1}(in last one – the number may be different depending on how many upload fields you have on your form and which one you include), it will include file URL only and will not attach image itself automatically.
Below the notification message editor there’s an option for “Attachments” which by default is set to “None”. Only after you change it to “Uploaded files” then the actual files will be included in message as real attachments;
Best regards,
Adamhi but the image is not showing at all , even in submission the upload-1 is also empty and the weird thing in email notification
You have a new website form submission:
{all_fields}{upload-1}
—
This message was sent from {site_url}.
the result turn out upload-1 showing my custom-field data (bank_account_number)<?php add_action('forminator_custom_form_submit_before_set_fields', 'wpmudev_change_hidden_fields_data', 10, 3); function wpmudev_change_hidden_fields_data($entry, $module_id, $field_data_array) { $form_ids = array(62); //Please change the form ID if (!in_array($module_id, $form_ids)) { return; } if (!is_user_logged_in()) { return; } $user = wp_get_current_user(); $user_data = get_userdata($user->ID); foreach ($field_data_array as $key => $value) { // HERE YOU MODIFY YOUR HIDDEN FIELDS VALUES # set user bio if (strpos($value['value'], 'bank_account_number') !== false) { Forminator_CForm_Front_Action::$info['field_data_array'][$key]['value'] = $user_data->bank_account_number; } } } add_filter('forminator_prepared_data', 'wpmudev_update_hidden_field_val_copy', 10, 2); function wpmudev_update_hidden_field_val_copy($prepared_data, $module_object) { $form_ids = array(62); //Please change the form ID if (!in_array($module_object->id, $form_ids)) { return $prepared_data; } foreach ($prepared_data as $key => $value) { // repeat this block as many times as the number of hidden fields that you modified with code // note: here you don't use field values but actual hidden fields IDs // of hidden fields that you modified. if (strpos($key, 'hidden-1') !== false) { $prepared_data[$key] = sanitize_text_field($_POST[$key]); } } return $prepared_data; }This is my code , without this the image url work properly , with this the image url wont work
are u here?
@wpmudev-support8 really appreciate if u can help me find out what happen to the file url not showing when I use this custom MU plugin for hidden field
Hi @hydevelop1994,
I’m marking this as resolved for now since you have created a new thread regarding the above queries here:
https://ww.wp.xz.cn/support/topic/custom-code-made-the-file-upload-image-url-dissapearPlease do follow up in the new thread for any further assistance.
Kind Regards,
Nithin
The topic ‘custom user meta in hidden fields?’ is closed to new replies.