Advertisement
Guest User

Display taxonomy name in entry

a guest
Jul 2nd, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. <?php
  2. /*
  3. 1. Edit your form in WP admin. Delete all values in you custom taxonomy input field.  Uncheck "enable values".
  4. 2. Goto "Advanced" tab and uncheck "Populate with taxonomy".  This will make values the same as label name.
  5. 3. Add the code below to your functions.php file.
  6. */
  7. add_action('gform_after_submission_YOURFORMID', 'gform_chnage_id_to_name', 10, 2);
  8. function gform_chnage_id_to_name($entry, $form) {
  9.  
  10.     $taxonomy = "YOURCUSTOMTAXONOMY";
  11.    
  12.     foreach( $form['fields'] as &$field ) {
  13.        
  14.         // In my case I use checkbox, but maybe it's different in you case.
  15.         if ( array_key_exists( 'type', $field ) && $field['type'] == 'checkbox' ) {
  16.  
  17.             $term_ids = array();
  18.  
  19.             foreach ( $field['inputs'] as $input ) {
  20.                
  21.                 // Get term data by label name
  22.                 $term = get_term_by('name', esc_html($input['label']), $taxonomy);
  23.                
  24.                 $term_name = $entry[ (string) $input['id'] ];
  25.                
  26.                 // Get term id
  27.                 if ( !empty($term_name) )
  28.                     $term_ids[] = (int) $term->term_id;
  29.                
  30.                 $i++;
  31.             }
  32.  
  33.             // Add selected types to post
  34.             if ( !empty ( $term_ids )) wp_set_object_terms( $entry['post_id'], $term_ids, $taxonomy, true );
  35.  
  36.         }
  37.        
  38.     }
  39.    
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement