Home / Disable / Disable Geolocation Current Location if the page URL contains Prefill by URL parameters targeting an address field
Duplicate Snippet

Embed Snippet on Your Site

Disable Geolocation Current Location if the page URL contains Prefill by URL parameters targeting an address field

When "Current Location" is enabled in WPForms > Settings > Geolocation, the geolocation JS overwrites address fields that were pre-filled via URL
parameters (Prefill by URL feature). This snippet detects whether the current page URL contains Prefill by URL parameters targeting an address field on the form being rendered. If so, it disables the Current Location feature for that page load, allowing the URL-based prefill values to persist.

Ralden Souza PRO
<10
Code Preview
php
<?php
/**
 * Fix: WPForms Prefill by URL takes priority over Geolocation Current Location.
 *
 * When "Current Location" is enabled in WPForms > Settings > Geolocation,
 * the geolocation JS overwrites address fields that were pre-filled via URL
 * parameters (Prefill by URL feature).
 *
 * This snippet detects whether the current page URL contains Prefill by URL
 * parameters targeting an address field on the form being rendered. If so,
 * it disables the Current Location feature for that page load, allowing the
 * URL-based prefill values to persist.
 *
 * HOW TO USE:
 * Add this snippet to your child theme's functions.php or via a code snippets plugin.
 *
 * REQUIRES:
 * - WPForms Pro
 * - WPForms Geolocation addon
 * - "Current Location" enabled in WPForms > Settings > Geolocation
 *
 */
add_filter( 'wpforms_geolocation_front_fields_settings_current_location', 'wpforms_disable_current_location_when_prefill_url_present' );
/**
 * Disable Geolocation Current Location when Prefill by URL params are present.
 *
 * Checks $_GET for any parameter matching the WPForms Prefill by URL pattern
 * targeting an address field (wpf{form_id}_{field_id}_{address_subfield}).
 * If a match is found, returns false to prevent Current Location from
 * overwriting the prefilled values.
 *
 * @param bool $current_location Whether Current Location is enabled.
 * @return bool
 */
function wpforms_disable_current_location_when_prefill_url_present( $current_location ) {
	// If Current Location is already off, nothing to do.
	if ( ! $current_location ) {
		return $current_location;
	}
	// No GET params means no Prefill by URL — bail early.
	if ( empty( $_GET ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		return $current_location;
	}
	// Address field subfields used by WPForms Prefill by URL.
	$address_subfields = [ 'address1', 'address2', 'city', 'state', 'postal', 'country' ];
	foreach ( $_GET as $key => $value ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		// Prefill by URL pattern: wpf{form_id}_{field_id}_{subfield}
		// e.g. wpf161_4_address1, wpf161_4_city
		if ( ! preg_match( '/^wpf(\d+)_(\d+)_(.+)$/i', $key, $matches ) ) {
			continue;
		}
		$subfield = sanitize_key( $matches[3] );
		if ( in_array( $subfield, $address_subfields, true ) ) {
			// An address subfield prefill param was found — disable Current Location.
			return false;
		}
	}
	return $current_location;
}

Comments

Add a Comment