Skip Navigation

[Resolved] Grab CRED field value in before_save_data hook

This support ticket is created 3 years, 8 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 6 replies, has 2 voices.

Last updated by julieP 3 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#1986733

I've always been able to grab the value entered in a single line/text CRED field using a before_save_data hook and do something with it like this:-

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 = $form_data['container_id'];
$value = $_POST['wpcf-custom-field'];
update_post_meta($post_id, 'wpcf-a-different-field', $value);
}
}

Is this approach no longer valid (it's not working)??

#1986831

Hi,

Thank you for contacting us and I'd be happy to assist.

The Toolset Type's custom fields are saved in the database with "wpcf-" prefix in the key, but that prefix is not used in the fields included in the front-end forms.

This is why while getting the value from the global $_POST, you'll only use the field's slug:


$value = $_POST['custom-field'];

regards,
Waqar

#1986945

Hi Waqar

OK I'll need to do some testing because I sought clarification about getting a timestamp from a CRED field in a before_save_data hook less than 12 months ago and Christian kindly explained the different formats for timestamps and text here https://toolset.com/forums/topic/date-field-2/#post-1723077 - you'll notice his examples DO use the wpcf prefix.

Something must have changed???

#1988421

Hi again

I've carried out more testing. Here's what I found:-

If I try getting the value like this, nothing happens

$value = $_POST['wpcf-custom-field'];

If I try getting the value like this, nothing happens and I get an "undefined index" entry for custom-field in my debug log

$value = $_POST['custom-field'];

Furthermore, when I hard coded the update_post_meta() function like this, the field wpcf-a-different-field isn't updated

$new_value = 'My New Value';
update_post_meta('9999', 'wpcf-a-different-field', $new_value);

Can you run the same tests yourself please and let me know what you find??

Thanks

BTW: I'm running WP 5.7, Types 3.4.7 and Forms 2.6.6

#1988687

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

#1989927

Waqar

I've run further tests and confirm it's now working as required.

Thank you for your assistance; as always, your explanation was clear and informative

#1989929

My issue is resolved now. Thank you!