Last active
March 24, 2021 17:08
-
-
Save tannermann/735a3daaf7c59eb75f0bcde956b1d238 to your computer and use it in GitHub Desktop.
Dynamic Checkbox Population not working with User Registration - entry data is not being saved into user meta - User Registration Add-On
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Save user registration entry data to ACF field when activating user based on their original submission | |
| add_action( 'gform_activate_user', 'after_user_activate', 10, 3 ); | |
| function after_user_activate( $user_id, $user_data, $signup_meta ) { | |
| $taxonomies = array( | |
| 'sport_type', | |
| 'age_range', | |
| 'school_year', | |
| ); | |
| $tax_terms_acf_field_key_on_user = ''; | |
| foreach($taxonomies as $taxonomy_name) { | |
| $tax_terms_string = get_user_meta($user_id, $taxonomy_name . '_terms', true); | |
| $tax_terms_slug_array = explode(', ', $tax_terms_string); | |
| $tax_terms_array = array(); | |
| foreach($tax_terms_array as $tax_term_slug) { | |
| $tax_term_object = get_term_by('slug', $tax_term_slug, $taxonomy_name); | |
| $term_id = $tax_term_object->term_id; | |
| $tax_terms_array[] = $term_id; | |
| } | |
| update_field( $taxonomy_name . '_terms', $tax_terms_array, 'user_' . $user_id ); | |
| } | |
| } | |
| // Helper function inside pk_populate_checkbox_tax_term_choices | |
| function pk_check_and_populate_checkboxes($tax_terms, $field) { | |
| $choices = []; | |
| $inputs = []; | |
| $input_id = 1; | |
| foreach( $tax_terms as $tax_term ) { | |
| //skipping index that are multiples of 10 (multiples of 10 create problems as the input IDs) | |
| if ( $input_id % 10 === 0 ) { | |
| $input_id++; | |
| } | |
| $choices[] = [ 'text' => $tax_term->name, 'value' => $tax_term->slug ]; | |
| $inputs[] = array( 'label' => $tax_term->name, 'id' => "{$field->id}.{$input_id}" ); | |
| $input_id++; | |
| } | |
| return array('choices' => $choices, 'inputs' => $inputs); | |
| } | |
| add_filter( 'gform_form_post_get_meta_2', 'pk_populate_checkbox_tax_term_choices' ); | |
| function pk_populate_checkbox_tax_term_choices( $form ) { | |
| $sport_type_args = [ | |
| 'taxonomy' => 'sport_type', | |
| 'order' => 'ASC', | |
| 'hide_empty' => false, | |
| ]; | |
| $sport_types = get_terms( $sport_type_args ); | |
| $age_range_args = [ | |
| 'taxonomy' => 'age_range', | |
| 'order' => 'ASC', | |
| 'hide_empty' => false, | |
| ]; | |
| $age_ranges = get_terms( $age_range_args ); | |
| $school_year_args = [ | |
| 'taxonomy' => 'school_year', | |
| 'order' => 'ASC', | |
| 'hide_empty' => false, | |
| ]; | |
| $school_years = get_terms( $school_year_args ); | |
| foreach( $form['fields'] as &$field ) { | |
| // Initialize variables | |
| $choices_and_inputs = array(); | |
| switch ($field->cssClass) { | |
| case 'sport_type': | |
| $choices_and_inputs = pk_check_and_populate_checkboxes($sport_types, $field); | |
| break; | |
| case 'age_range': | |
| $choices_and_inputs = pk_check_and_populate_checkboxes($age_ranges, $field); | |
| break; | |
| case 'school_year': | |
| $choices_and_inputs = pk_check_and_populate_checkboxes($school_years, $field); | |
| break; | |
| } | |
| if( $field->cssClass === 'sport_type' || $field->cssClass === 'age_range' || $field->cssClass === 'school_year' ) { | |
| $field->choices = $choices_and_inputs['choices']; | |
| $field->inputs = $choices_and_inputs['inputs']; | |
| } | |
| } | |
| return $form; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment