Skip Navigation

[Resolved] Conditional cred save data based on cred generic field

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

Problem:
Add a condition in Cred save data function to check the radio button:
if it is set on "approve", then I update the post status from "1" to "2"
if it is set on "refuse", then I update the post status from "1" to "0"

Solution:
Please add this code in your theme’s or child theme’s functions.php file:

add_action('cred_save_data', 'prefix_cred_save_data', 10, 2);
function prefix_cred_save_data( $post_id, $form_data ){
    if ($form_data['id']==1158){ //do the following code only if the CRED form ID is 1158, adjust this to your CRED form ID
        if(isset($_POST['my-generic-field']) && $_POST['my-generic-field'] != '') { // If generic value is set
            $generic_field_value = $_POST['my-generic-field'] == 'approve' ? 1 : 0; // if generic value is approve then return 1 otherwise 0
        }
        else {
            $generic_field_value = 2; // If generic value is not set
        }
         
        update_post_meta($post_id, 'wpcf-etat-de-la-demande', $generic_field_value, true); // update custom field
             
    }
}

==> Look for the comments in the above code and change your Form ID, Custom Field slug, etc as needed.

This support ticket is created 7 years, 3 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
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by romanB-3 7 years, 3 months ago.

Assisted by: Noman.

Author
Posts
#555555

Hello,

I have set a cred form with a cred generic radio button field with 2 options : "approve"/"refuse".

I also have a cred save data function that stores the status of the post : 0=refused, 1=step1, 2=step2

I need to add a condition in my cred save data function to check the radio button :
if it is set on "approve", then I update the post status from "1" to "2"
if it is set on "refuse", then I update the post status from "1" to "0"

Here is the function

add_action('cred_save_data', 'sauver_etat_commande_niv3',10,2);
function sauver_etat_commande_niv3($post_id, $form_data)
{if ($form_data['id']==273) {update_post_meta($post_id, 'wpcf-etat-de-la-demande', '3', true);}}

I wonder what would be the best way to add this condition ?

Thank you very much.

#555597

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Roman,

Thank you for contacting Toolset support. Please add this code in your theme’s or child theme’s functions.php file and it will resolve the issue:

add_action('cred_save_data', 'prefix_cred_save_data', 10, 2);
function prefix_cred_save_data( $post_id, $form_data ){
    if ($form_data['id']==1158){ //do the following code only if the CRED form ID is 1158, adjust this to your CRED form ID
		if(isset($_POST['my-generic-field']) && $_POST['my-generic-field'] != '') { // If generic value is set
			$generic_field_value = $_POST['my-generic-field'] == 'approve' ? 1 : 0; // if generic value is approve then return 1 otherwise 0
		}
		else {
			$generic_field_value = 2; // If generic value is not set
		}
		
		update_post_meta($post_id, 'wpcf-etat-de-la-demande', $generic_field_value, true); // update custom field
			
    }
}

==> Look for the comments in the above code and change your Form ID, Custom Field slug, etc as needed.

Thank you

#555733

Worked great. Thank you very much.