Hi,
Thank you for sharing your results and I stand corrected.
In my tests, I noted that the "wpcf-" prefix will be needed to get values from the global $_POST.
$value = $_POST['wpcf-field-slug'];
There are a couple of other points which should be carefully considered:
1. The line "$post_id = $form_data['container_id']" would set the ID of the page/post on which the form has been placed and not the ID of the page/post which is being added/edited using the form.
If your goal is to update the custom field value of the page/post that is being added/edited using the form, then you should use:
$post_id = $_POST['_cred_cred_prefix_post_id'];
2. Since in this case, the hook being used is "cred_before_save_data", it is important to see whether the field "a-different-field" (whose value you need to update programmatically) is available in the form or not. If that field is also available in the form, the value that will be actually passed on the form field, will overwrite your value set through the function attached to the "cred_before_save_data" hook.
(because this function will execute before the form's data is saved, but, when the form's data will be saved afterward, it will use the actual value from the form field)
Suppose you have two single-line fields "single-line-field-a" and "single-line-field-b".
a). To update the value from "single-line-field-a" into the field "single-line-field-b", when the field "single-line-field-b" is not present in the form, you'll use:
add_action('cred_before_save_data', 'before_save_data_form_3966',10,2);
function before_save_data_form_3966($form_data) {
if ($form_data['id']==3966) {
$post_id = $_POST['_cred_cred_prefix_post_id'];
$value = $_POST['wpcf-single-line-field-a'];
update_post_meta($post_id, 'wpcf-single-line-field-b', $value);
}
}
b). To update the value from "single-line-field-a" into the field "single-line-field-b", when the field "single-line-field-b" is also present in the form, you'll simply use:
add_action('cred_before_save_data', 'before_save_data_form_3966',10,2);
function before_save_data_form_3966($form_data) {
if ($form_data['id']==3966) {
$_POST['wpcf-single-line-field-b'] = $_POST['wpcf-single-line-field-a'];
}
}
In this case, when the form's data will be saved afterward, it will store the "single-line-field-b" value the same as the "single-line-field-a".
regards,
Waqar