Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- 1. Edit your form in WP admin. Delete all values in you custom taxonomy input field. Uncheck "enable values".
- 2. Goto "Advanced" tab and uncheck "Populate with taxonomy". This will make values the same as label name.
- 3. Add the code below to your functions.php file.
- */
- add_action('gform_after_submission_YOURFORMID', 'gform_chnage_id_to_name', 10, 2);
- function gform_chnage_id_to_name($entry, $form) {
- $taxonomy = "YOURCUSTOMTAXONOMY";
- foreach( $form['fields'] as &$field ) {
- // In my case I use checkbox, but maybe it's different in you case.
- if ( array_key_exists( 'type', $field ) && $field['type'] == 'checkbox' ) {
- $term_ids = array();
- foreach ( $field['inputs'] as $input ) {
- // Get term data by label name
- $term = get_term_by('name', esc_html($input['label']), $taxonomy);
- $term_name = $entry[ (string) $input['id'] ];
- // Get term id
- if ( !empty($term_name) )
- $term_ids[] = (int) $term->term_id;
- $i++;
- }
- // Add selected types to post
- if ( !empty ( $term_ids )) wp_set_object_terms( $entry['post_id'], $term_ids, $taxonomy, true );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement