Skip Navigation

[Resolved] multiple cred_save_data calls, only one working

This thread is resolved. Here is a description of the problem and solution.

Problem:

Save a generic field value into database with custom PHP codes.

Solution:

The action hook "cred_save_data" will be triggered after user submit the editing/creating post form, see our document:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Relevant Documentation:

This support ticket is created 4 years, 1 month 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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by jamesR-13 4 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1884101

I have multiple forms using generic fields to update the data in types text fields. The add post form cred_save_data call works as expected, but the edit post form cred_save_data call doesn't seem to be firing. The code for both is below. The call for form ID 338 is working as expected, the one for form ID 344 is not. They both reference the same fields and the same post type. Please advise:

//-------copy values from generic select to text field

add_action('cred_save_data', 'update_status',10,2);
function update_staus($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==344)
{
error_log('cred save data, form 344');
if (isset($_POST['unitstatusselect']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-unitstatus', $_POST['unitstatusselect'], true);
}
}
}

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==338)
{
error_log('cred save data, form 338');
if (isset($_POST['unitstatusselect']))
{
// add it to saved post meta
update_post_meta($post_id, 'wpcf-unitstatus', $_POST['unitstatusselect'], true);
}
}
}

#1884699

Hello,

The action hook "cred_save_data" will be triggered after user submit the editing/creating post form, see our document:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

And since you are editing/creating the posts of same post type/custom field, you can merge both functions into one, for example:

add_action('cred_save_data', 'update_status',10,2);
function update_status($post_id, $form_data)
{
	// if a specific form
	$arr = array(344, 338);
	if (in_array( $form_data['id'], $arr) )
	{
		//error_log('cred save data, form 344');
		if (isset($_POST['unitstatusselect']))
		{
			// add it to saved post meta
			update_post_meta($post_id, 'wpcf-unitstatus', $_POST['unitstatusselect'], true);
		}
	}
}
#1885259

I tried your code and now neither of my forms is updating the unitstatus meta field. Please advise.

#1885519

Is there another supporter available in a different time zone? I'm in GMT -7

#1885711

Unfortunately, due to holiday issue, we have few supporters online, you can provide a test site with the same problem, fill below private message box with login details and FTP access, also point the problem page URL, and where I can edit your custom PHP codes, I need a live website to test and debug this issue, thanks

#1886463

Thanks for the details, I have changed the custom codes in your theme file "functions.php", line 40 from:

function update_staus($post_id, $form_data)

To:

function update_unit_status($post_id, $form_data)

Please test again, check if it is fixed.

#1886877

The add new post form seems to set the field value again now, but the edit post form is still not updating the unit-status field.

#1888455

I have changed line 47 of your custom PHP codes from:
update_post_meta($post_id, 'wpcf-unitstatus', $_POST['unitstatusselect'], ture);

To:
update_post_meta($post_id, 'wpcf-unitstatus', $_POST['unitstatusselect']);

Please test again, check if it is fixed.

More help:
https://developer.wordpress.org/reference/functions/update_post_meta/

#1889507

Thank you