Remember Location Cookie

This topic contains 8 replies, has 3 voices, and was last updated by  Alex Rollin 5 years, 2 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #507791

    Dave
    Full Member
    Post count: 205

    Hi guys,

    I’m trying to create a cookie which remembers a users location so that when they come back to the site, it’ll pick up the location they were previously looking at.

    I was hoping you can help us with what we need to do this?

    We need the cookie to read which location they select on the location selector, and then when user returns to the site at later date, read that cookie and redirect them to previous session location.

    I need to know the hooks and any filters to use in order to do it.

    Thanks,
    Dave

    #507815

    Alex Rollin
    Moderator
    Post count: 27815

    Hello,

    GeoDirectory doesn’t use cookies or sessions. Location history is kept in the browser local storage.

    To access that will require custom JS see this to get started

    https://developers.google.com/web/tools/chrome-devtools/storage/localstorage

    #507834

    Dave
    Full Member
    Post count: 205

    Thanks Alex.

    The reason I asked about cookies was because of this previous forum thread where one of your team advised to create a cookie…
    https://wpgeodirectory.com/support/topic/remember-location/

    The other admin said it was a few lines of code, but writing javascript which automatically gets the localStorage then sends it to the server for a redirect is far more than that.

    In your FAQ’s, you say that there are many hooks and filters and just to ask you for the right ones for what we’re trying to customise:
    https://wpgeodirectory.com/docs-v2/faq/customizing/#hooks-filters

    Are you able to provide the hooks please?

    Thanks,
    Dave

    #507863

    Alex Rollin
    Moderator
    Post count: 27815

    The developers have been alerted to your request.

    #507865

    Dave
    Full Member
    Post count: 205

    Ok thank you.

    #507991

    Kiran
    Moderator
    Post count: 7069

    Hi Dave,

    Try following PHP snippet to store location in cookie.

    
    
    function gd_snippet_190913_set_current_location( $location ) {
    	if ( ! empty( $location->type ) && geodir_is_page( 'location' ) ) {
    		// DO YOUR STUFF HERE
    	}
    
    	return $location;
    }
    add_filter( 'geodir_set_current_location', 'gd_snippet_190913_set_current_location', 10, 1 );
    

    Regards,
    Kiran

    #508918

    Dave
    Full Member
    Post count: 205

    Hi Kiran, thanks very much for that. This is what we’ve written so far…

    function gd_snippet_190913_set_current_location( $location ) {
            if ( ! empty( $location->type ) && geodir_is_page( "location" ) ) {
                    setcookie("last_location",base64_encode(serialize($location)));
            }
            return $location;
    }
    add_filter( "geodir_set_current_location", "gd_snippet_190913_set_current_location", 10, 1 );

    How would we get the value that we’ve written into the cookie, into the user session? For example:

    add_action( 'init', 'process_post' );
    
    function process_post() {
    
         if( isset($_COOKIE['last_location']) ) {
    
              $_SESSION[‘YOUR_KEY_HERE’] = unserialize(base64_decode($_COOKIE[‘last_location’]));
    
         }
    
    }
    

    What is the session key that we need please?

    Thanks,
    Dave

    #508925

    Kiran
    Moderator
    Post count: 7069

    Hi David,

    To set location in cookie use following code.

    setcookie( 'last_location', base64_encode( maybe_serialize( $location ) ), time() + YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );

    How would we get the value that we’ve written into the cookie, into the user session?

    To get value stored from cookie use following code.

    $last_location = ! empty( $_COOKIE['last_location'] ) ? maybe_unserialize( base64_decode( $_COOKIE['last_location'] ) ) : '';

    What is the session key that we need please?

    You can any session key you want.

    Kiran

    #508943

    Alex Rollin
    Moderator
    Post count: 27815

    GD doesn’t use sessions so you would need to use your own.

Viewing 9 posts - 1 through 9 (of 9 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
20% Discount Offer
Hurry! Get your 20% discount before it expires. Get 20% Discount