Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //After Submission hook & function
- add_action("gform_after_submission", "after_submission", 10, 2);
- function after_submission($entry, $form){
- //For this example, i was creating a simple post (of course it can be used for anything from complex woocommerce production creation to user registration.
- $my_post = array(
- 'post_title' => wp_strip_all_tags( 'test' ),
- 'post_content' => 'Test',
- 'post_status' => 'publish'
- );
- // Insert the post into the database
- $post_id = wp_insert_post( $my_post );
- //First define the Form ID and Post Meta Title.
- $fieldID = 2; //Gravity Forms Field ID
- $fieldName = 'checkbox-test'; //Wp-Types field slug
- //Now pass all 5 parameters to the checkbox function ($form & $entry are defined in this hook)
- gform_wpcf_checkbox_selector($post_id, $form, $fieldID, $entry, $fieldName);
- }
- /**
- * GravityFroms to WP-Types selector
- */
- function gform_wpcf_checkbox_selector($post_id = '', $form = '', $fieldID = '', $entry = '', $wpcf_field = ''){
- //if any values not passed, abort
- if ($post_id == '' || $form == '' || $fieldID == '' || $entry == '' || $entry == '') {
- return;
- }
- //Get array of Checkbox Names and Field IDs
- foreach ($form['fields'] as $key => $value) {
- //Loop through fields and look for fieldID
- if ($value['id'] == $fieldID) {
- //once we locate the correct field, create a new array of the values held.
- $gormFields = $form['fields'][$key]['inputs'];
- }
- }
- //Get WP-Types field details and set blank $options array (holds the selected values for updating)
- require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
- $fieldData = wpcf_admin_fields_get_field($wpcf_field);
- $options = array();
- //loop through the options in the wpcf-field array
- foreach ($fieldData['data']['options'] as $key => $checkboxField) {
- //while looping through gformFields array for each options entry
- foreach ($gormFields as $keyForm => $valueForm) {
- //Check if the titles match and if the field has a value defined (this can be anything set in the gravity form)
- if ($valueForm['label'] == $checkboxField['title'] && $entry[$valueForm['id']] != '') {
- //If both criteria is matched, create row in $options array and defined as 'set_value' from wpcf data
- $options[$key] = array($checkboxField['set_value']);
- }
- }
- }
- //update meta field
- update_post_meta($post_id, 'wpcf-'.$wpcf_field, $options);
- }
Add Comment
Please, Sign In to add comment