Guest User

Untitled

a guest
Jan 7th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. <?php
  2. //After Submission hook & function
  3. add_action("gform_after_submission", "after_submission", 10, 2);
  4.  
  5. function after_submission($entry, $form){
  6.  
  7. //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.
  8.  
  9. $my_post = array(
  10. 'post_title' => wp_strip_all_tags( 'test' ),
  11. 'post_content' => 'Test',
  12. 'post_status' => 'publish'
  13. );
  14.  
  15. // Insert the post into the database
  16. $post_id = wp_insert_post( $my_post );
  17.  
  18. //First define the Form ID and Post Meta Title.
  19. $fieldID = 2; //Gravity Forms Field ID
  20. $fieldName = 'checkbox-test'; //Wp-Types field slug
  21. //Now pass all 5 parameters to the checkbox function ($form & $entry are defined in this hook)
  22. gform_wpcf_checkbox_selector($post_id, $form, $fieldID, $entry, $fieldName);
  23.  
  24. }
  25.  
  26. /**
  27. * GravityFroms to WP-Types selector
  28. */
  29.  
  30. function gform_wpcf_checkbox_selector($post_id = '', $form = '', $fieldID = '', $entry = '', $wpcf_field = ''){
  31.  
  32. //if any values not passed, abort
  33. if ($post_id == '' || $form == '' || $fieldID == '' || $entry == '' || $entry == '') {
  34. return;
  35. }
  36.  
  37. //Get array of Checkbox Names and Field IDs
  38. foreach ($form['fields'] as $key => $value) {
  39. //Loop through fields and look for fieldID
  40. if ($value['id'] == $fieldID) {
  41. //once we locate the correct field, create a new array of the values held.
  42. $gormFields = $form['fields'][$key]['inputs'];
  43. }
  44. }
  45.  
  46. //Get WP-Types field details and set blank $options array (holds the selected values for updating)
  47. require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
  48. $fieldData = wpcf_admin_fields_get_field($wpcf_field);
  49. $options = array();
  50.  
  51. //loop through the options in the wpcf-field array
  52. foreach ($fieldData['data']['options'] as $key => $checkboxField) {
  53. //while looping through gformFields array for each options entry
  54. foreach ($gormFields as $keyForm => $valueForm) {
  55. //Check if the titles match and if the field has a value defined (this can be anything set in the gravity form)
  56. if ($valueForm['label'] == $checkboxField['title'] && $entry[$valueForm['id']] != '') {
  57. //If both criteria is matched, create row in $options array and defined as 'set_value' from wpcf data
  58. $options[$key] = array($checkboxField['set_value']);
  59. }
  60. }
  61. }
  62.  
  63. //update meta field
  64. update_post_meta($post_id, 'wpcf-'.$wpcf_field, $options);
  65.  
  66. }
Add Comment
Please, Sign In to add comment