Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a form with some checkboxes and inputboxes selectboxes and it shows what the user wants via an ajax call. The problem is that when the user click on the item and the detail page is shown and then decide to go back to the previous page he needs to click and select his previous choice again.

I would like to make WP to store all the options in the session when the button to the detail page is clicked and save actual info in the session and then when he visits the page again the values will be chcecked in sessions and set if any are found.

Could that be done in WP?

If yes, how?

Let's oversimplify that and say that we have something like this in our form:

<input class="car_color" type="checkbox" name="car_color" value="1" />
<input class="car_color" type="checkbox" name="car_color" value="2" />
<input class="car_color" type="checkbox" name="car_color" value="8" />
<input class="car_color" type="checkbox" name="car_color" value="4" />
<input class="car_color" type="checkbox" name="car_color" value="6" />

I don't use submit button in my form, it's handled via AJAX on input change.

And in my results get via ajax I have a link to the detail page:

<a class="detail-info-link" href="<?php echo $url ?>">

Any idea how can I store my values in Session and call them when revisiting/reloading/back button in the browser?

I need to be able to read the session stored stuff and use it via ?Javascript? and trigger my search fuction via ajax which is already working well.

I just need to store (probably before going to $link in href of by detail button and read and send session variables (if they exist).

share|improve this question

1 Answer 1

Sessions aren't enabled in wordpress by default, if you want to activate php sessions add this at the beginning of your functions.php:

if (!session_id()) {
    session_start();
}

You now can use $_SESSION['your-var'] = 'your-value'; to set a session variable. Take a look at the PHP documentation on sessions.



Update:

There was a second answer, which, in my mind, had a value too - unfortunately it got deleted, I'm re-adding the information here. The answer was referring to WP Session Manager a plugin written by @eamann as a alternative solution.
I read some things about the plugin, but never used it because - so far - I'm sticking with PHP sessions - never had the problem that I couldn't use them. This is a statement/comment by the plugin author himself I found about some possible advantages.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.