Skip to content

Instantly share code, notes, and snippets.

@EddyRespondek
Created May 31, 2017 02:04
Show Gist options
  • Save EddyRespondek/8172ebcaa01488527fc111bf375b3eaf to your computer and use it in GitHub Desktop.
Save EddyRespondek/8172ebcaa01488527fc111bf375b3eaf to your computer and use it in GitHub Desktop.
GF Suburb Select
// Gravity Forms - Suburb Auto Suggest.
add_filter( 'gform_add_field_buttons', 'add_suburbs_field' );
function add_suburbs_field( $field_groups ) {
foreach ( $field_groups as &$group ) {
if ( $group['name'] == 'advanced_fields' ) {
$group['fields'][] = array(
'class' => 'button',
'data-type' => 'suburbs',
'value' => __( 'Suburbs', 'gravityforms' ),
'onclick' => "StartAddField('suburbs');"
);
break;
}
}
return $field_groups;
}
add_filter( 'gform_field_type_title', 'add_suburbs_title', 10, 2 );
function add_suburbs_title( $title, $field_type ) {
if ( $field_type == 'Suburbs' ) {
return 'Suburbs';
}
return $title;
}
add_action( "gform_editor_js", "wps_gform_editor_js" );
function wps_gform_editor_js() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
fieldSettings["suburbs"] = ".label_setting, .description_setting, .css_class_setting, .visibility_setting, .conditional_logic_field_setting";
});
</script>
<?php
}
add_filter( 'gform_field_input', 'suburbs_input', 10, 5 );
function suburbs_input( $input, $field, $value, $lead_id, $form_id ) {
$suburbs_html = '';
if ( $field->type == 'suburbs' ) {
if( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$saved_suburbs = get_user_meta($current_user->ID, 'suburbs', true);
if ( $saved_suburbs && !empty($saved_suburbs) ) {
$saved_suburbs = explode(',', $saved_suburbs);
$i = 0;
foreach( $saved_suburbs as $key => $saved_suburb ) {
$i++;
$suburbs_html .= '<li class="gchoice_' . $form_id . '_' . $field->id . '_' . $i . '"><input autocomplete="off" name="input_' . $field->id . '.' . $i . '" type="checkbox" value="' . esc_attr($saved_suburb) . '" id="choice_' . $form_id . '_' . $field->id . '_' . $i . '" tabindex="8" checked="checked"><label for="choice_' . $form_id . '_' . $field->id . '_' . $i . '" id="label_' . $form_id . '_' . $field->id . '_' . $i . '">' . $saved_suburb . '</label></li>';
}
}
}
$input = '<div class="ginput_container ginput_container_suburbs" data-form="' . $form_id . '" data-field="' . $field->id . '">';
$input .= '<input type="text" value="" name="suburbs" id="suburbs_lookup" placeholder="Search suburbs..." autocomplete="off" class="">';
$input .= '<div id="suburb_picklist"><ul></ul></div>';
$input .= '<div id="suburb_selection"><span class="title">Selected Suburbs</span><ul class="" id="input_' . $form_id . '_' . $field->id . '">';
$input .= $suburbs_html;
$input .='</ul></div>';
$input .= '</div>';
}
return $input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment