parkeast

Gravity Forms Checkbox Save to Taxonomies

Aug 16th, 2012
390
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Add this into your functions.php file for the form.
  2. // This part populates the form with the data already in the database.
  3. // You should edit everything that is IN_ALL_CAPS.
  4. $YOUR_TERMS_VAR = get_the_terms( $post->ID, 'YOUR_TAXONOMY' );
  5. if ( $YOUR_TERMS_VAR && ! is_wp_error( $YOUR_TERMS_VAR ) ) :
  6.     $SELECTED_ITEMS = array();
  7.     foreach ( $YOUR_TERMS_VAR as $term ) {
  8.         //use "slug" instead of "id" or "value"
  9.         $SELECTED_ITEMS[] = $term->slug;
  10.     }
  11.     $teachers = join( ", ", $SELECTED_ITEMS );
  12. endif;
  13. $current_student_data['SELECTED_ITEMS'] = $teachers;
  14.  
  15.  
  16. // you may also use "gform_after_submission_##" for a specific form.
  17. add_action( 'gform_after_submission', 'gf_edit_student', 10, 2 );
  18. function gf_edit_student( $entry, $form ) {
  19.     global $post;
  20.     global $wpdb;
  21.     global $wp_query;
  22.     $postID = $wp_query->post->ID;
  23.  
  24.  
  25. // YOUR_TERMS_VAR should match what is above.
  26. // This part saves the new data to the database.
  27. // the "." [dot] after the 30 (which should be your ID of your checkbox field) stays.
  28.         $YOUR_TERMS_VAR = array();
  29.         foreach($entry as $field=>$slug){
  30.             if (substr($field, 0, 3) == '30.' ) { //30. = the id of your checkboxes
  31.                 $YOUR_TERMS_VAR[] = $slug;
  32.             }
  33.         }
  34.  
  35.  
  36. // YOUR_TERMS_VAR should match what is above.
  37. // Edit "YOUR_TAXONOMY" to be your taxonomy name.
  38. wp_set_object_terms($postID, $YOUR_TERMS_VAR, 'YOUR_TAXONOMY');
RAW Paste Data