Created
October 16, 2025 20:47
-
-
Save yuriinalivaiko/7bcfda1feaf971381e465ff39aa67583 to your computer and use it in GitHub Desktop.
This code snippet replaces the full address with the city name in the User Location field.
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 | |
| /** | |
| * Change the user location field value. Save only a city. | |
| */ | |
| add_action( 'wp_footer', function () { | |
| ?><script type="text/javascript"> | |
| var currentLocationField; | |
| jQuery( '.um_user_location_g_autocomplete' ).on( 'click', function() { | |
| currentLocationField = this; | |
| } ); | |
| // on autocomplate. | |
| wp.hooks.addAction( 'um_user_location_after_location_field_set_by_autocomplete', 'um_user_locations', function ( place ) { | |
| for (const component of place.address_components) { | |
| if ( 'locality' === component.types[0] ) { | |
| currentLocationField.value = component.long_name; | |
| break; | |
| } | |
| if ( 'postal_town' === component.types[0] ) { | |
| currentLocationField.value = component.long_name; | |
| break; | |
| } | |
| if ( 'administrative_area_level_3' === component.types[0] ) { | |
| currentLocationField.value = component.long_name; | |
| } | |
| } | |
| } ); | |
| // on geocode. | |
| wp.hooks.addFilter( 'um_user_location_formatted_address_set_by_geocode', 'um_user_locations', function( formatted_address, results, position ) { | |
| for (const component of results[0].address_components) { | |
| if ( 'locality' === component.types[0] ) { | |
| return component.long_name; | |
| } | |
| if ( 'postal_town' === component.types[0] ) { | |
| return component.long_name; | |
| } | |
| if ( 'administrative_area_level_3' === component.types[0] ) { | |
| return component.long_name; | |
| } | |
| } | |
| return formatted_address; | |
| } ); | |
| </script><?php | |
| }, 20 ); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist is a part of the article User Locations - Hooks
You can add this code to the
functions.phpfile in the active theme directory. Skip the opening<?phptag.