• Resolved ashikai

    (@ashikai)


    Hello!
    I have a site that uses post proximity search feature to find certain post types within x-miles of the user’s entered location. This already works out of the box, but what I can’t quite figure out is how to make that search persistent and/or how to save that search value as the user’s location.

    I’m not asking for a “how do I” so much as I’d just like to know where I should be looking in the documentation or if this is even documented as of yet (I know from reading other threads that the documentation is a work in progress, so this may have just not been included yet). I saw some references to saving a user’s location in the documentation, but I did not see any about saving the value of the search field as a user’s location.

    Any chance you can point me at the function or hook that I need to look at in order to make this happen?

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Eyal Fitoussi

    (@ninjew)

    Hello @ashikai,

    What you described is not available out of the box.

    By default, the user’s current location can be recorded using the Auto locator feature ( can be enabled in the Settings page ) and/or using the Current Location shortcode/widget.

    You can try the function below and see if it does what you need. The function should save the address and coordinates as the user’s current location whenever the form is submitted with an address:

    
    function gmw_custom_update_current_location() {
    
    	// Abort if location is missing.
    	if ( empty( $_GET['action'] ) || 'fs' !== $_GET['action'] || empty( $_GET['lat'] ) || empty( $_GET['lng'] ) || empty( $_GET['address'] ) ) {
    		return;
    	}
    
    	$user_location = array(
    		'street'            => '',
    		'city'              => '',
    		'region_name'       => '',
    		'region_code'       => '',
    		'postcode'          => '',
    		'country_name'      => '',
    		'country_code'      => '',
    		'address'           => $_GET['address'][0],
    		'formatted_address' => $_GET['address'][0],
    		'lat'               => $_GET['lat'],
    		'lng'               => $_GET['lng'],
    	);
    
    	$cache = (object) array();
    
    	// save location fields.
    	foreach ( $user_location as $field => $value ) {
    
    		// clear cookie.
    		unset( $_COOKIE[ "gmw_ul_{$field}" ] );
    		setcookie( "gmw_ul_{$field}", '', time() - 300 );
    
    		// save new value if exists.
    		if ( ! empty( $value ) ) {
    
    			// Sanitize the value.
    			$value         = sanitize_text_field( stripslashes( $value ) );
    			$cache->$field = $value;
    
    			setcookie( "gmw_ul_{$field}", $value, strtotime( '+7 days' ), '/' );
    		} else {
    			$cache->$field = '';
    		}
    	}
    
    	// save user location in cache.
    	wp_cache_set( 'gmw_user_crrent_location', $cache, '', 86400 );
    }
    add_action( 'wp_footer', 'gmw_custom_update_current_location', 99 );
    

    I hope this helps.

    Thread Starter ashikai

    (@ashikai)

    Thank you! I’ll give this a try and report back after some troubleshooting!

    Plugin Author Eyal Fitoussi

    (@ninjew)

    You are welcome @ashikai.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Passing and saving user location from the search form’ is closed to new replies.