Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matty0501/8e80a022efce31ff8bdcfa08310bc52c to your computer and use it in GitHub Desktop.
Save matty0501/8e80a022efce31ff8bdcfa08310bc52c to your computer and use it in GitHub Desktop.
<?php
/**
* This is a modified version of this snippet: https://github.com/gravitywiz/snippet-library/blob/master/gp-address-autocomplete/gpaa-show-place-name.php
*
* Find addresses by their name and prepend place name to address line 1.
* Also allows searching by UK post codes and does not populate place name if this is equal to post code.
*
* Developer's Note: The JS in this snippet cannot be included via the Custom Javascript plugin due
* to an order-of-events issue with GPAA calling the options filter before the Custom Javascript plugin
* has initialized.
*/
// Update "123" to your form ID or remove "_123" to apply to all forms.
add_action( 'gform_pre_enqueue_scripts_123', function() {
?>
<script>
// Enable search by Place Name
gform.addFilter( 'gpaa_autocomplete_options', function( options ) {
options.types = [ 'geocode', 'establishment' ];
options.fields.push( 'name' );
return options;
} );
// Display Place Name
gform.addAction('gpaa_fields_filled', function ( place, instance, formId, fieldId ) {
var p = place.name;
var r = '';
var s = '';
place.address_components.forEach( function( component ) {
if ( component.types.indexOf( 'route' ) !== -1 ) {
r = component.long_name;
}
if ( component.types.indexOf( 'postal_code' ) !== -1 ) {
if( component.long_name == p ){
p = '';
}
}
});
if( p !== '' && r !== '' ){
s = ', ';
}
// Update "123" to your form ID, update _1_1 to your field ID. If your field ID is 4, this would be _4_1.
// You must specify the form and field IDs, even if the snippet applies to all forms.
jQuery('#input_123_1_1').val( p + s + r );
return place;
} );
</script>
<?php
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment