Skip to content

Instantly share code, notes, and snippets.

@jetsloth
Created August 15, 2020 01:55
Show Gist options
  • Save jetsloth/99c187532ec83563415c085bf597a67d to your computer and use it in GitHub Desktop.
Save jetsloth/99c187532ec83563415c085bf597a67d to your computer and use it in GitHub Desktop.
Add color choices to field dynamically populated by the Gravity Forms + Custom Post Types plugin, where colors are set as the text of each taxonomy term
<?php
function add_colors_to_taxonomy_choices( $form ) {
global $gf_cpt_addon;
if ( empty($gf_cpt_addon) ) {
return $form;
}
foreach( $form['fields'] as &$field ) {
$taxonomy = $gf_cpt_addon->get_field_taxonomy( $field );
$colorPickerEnabled = ( isset($field['colorPicker_enableColors']) && !empty($field['colorPicker_enableColors']) );
if ( $taxonomy && $colorPickerEnabled ) {
$new_field_choices = array();
foreach( $field['choices'] as $choice ) {
$term_id = $choice['value'];
$term_name = $choice['text'];
if ( !empty($term_id) ) {
$color_hex = $term_name;// UPDATE THIS TO CORRECT HEX VALUE WITH # (eg #ffffff)
$choice['colorPicker_color'] = $color_hex;
}
$new_field_choices[] = $choice;
}
$field['choices'] = $new_field_choices;
}
}
return $form;
}
add_filter( 'gform_pre_render', 'add_colors_to_taxonomy_choices', 100, 1 );
add_filter( 'gform_pre_submission_filter', 'add_colors_to_taxonomy_choices', 100, 1 );
add_filter( 'gform_admin_pre_render', 'add_colors_to_taxonomy_choices', 100, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment