Search code examples
phphtmlwordpresspostget

(WordPress) Keep get method/parameters through multiple pages


Is it possible to hold the get params. in the url by switching pages?

Situation: At the first page the user can choose between different prices. The "submit" button is just an easy

<a href="/genrepage?price=twoDollars" value.....>

So the user does get directed from the ".../pricepage" to the ".../genrepage?price=twoDollars" url. At this point it is possible to choose between different genres. The submit button is in a ..

<form method="get" action="">
<input type="submit" name="genre" value="action">
</form>

..now the user does get redirected to the ".../genrepage?genre=action" url. I would like to see something like ".../genrepage?price=twoDollars&genre=action.

Is it possible to realise this via WordPress (only) or would I need to code "smthg like a" plugin with php? Or is the price get param. saved in the $_Session so I could call it anyhow in the functions.php?

(Sry for this english and my bad knowledge, still learning^^)


Solution

  • You are obviously a web dev noob but we all have been in earlier times 😉 well in case you have a session opened (WordPress does not use sessions by default) you simply could do:

    $_SESSION["price"] = $_GET["price"];
    

    You also could start a session if none exists by adding

    session_start();
    

    in the beginning of your functions.php file.

    The get variable is saved to the session then and will be available in your functions.php (and elswhere) as long as the session is alive. Save into your session whatever get/post variables you want.

    If you are not using sessions you could put the price value into a hidden field at the form in the second page:

    <form method="get" action="">
    <!-- ... your other fields ... -->
    <input type="hidden" name="price" value="<?php echo htmlentities($_GET["price"]); ?>" />
    <input type="submit" name="genre" value="action">
    </form>
    

    Then on the next page both variables will available in $_GET.