Remember Location Cookie
This topic contains 8 replies, has 3 voices, and was last updated by Alex Rollin 6 years, 1 month ago.
We have moved to a support ticketing system and our forums are now closed.
Open Support Ticket-
AuthorPosts
-
September 12, 2019 at 10:37 am #507791
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,
DaveSeptember 12, 2019 at 12:20 pm #507815Hello,
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
September 12, 2019 at 1:37 pm #507834Thanks 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-filtersAre you able to provide the hooks please?
Thanks,
DaveSeptember 12, 2019 at 3:40 pm #507863The developers have been alerted to your request.
September 12, 2019 at 3:42 pm #507865Ok thank you.
September 13, 2019 at 9:27 am #507991Hi 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,
KiranSeptember 18, 2019 at 10:38 am #508918Hi 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,
DaveSeptember 18, 2019 at 11:12 am #508925Hi 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
September 18, 2019 at 1:15 pm #508943GD doesn’t use sessions so you would need to use your own.
-
AuthorPosts
We have moved to a support ticketing system and our forums are now closed.
Open Support Ticket