1

I've created a button on my Wordpress site which allows my users to change their location from 'London' to 'Manchester'.

The button uses $_POST to start a PHP function.

As part of that PHP function I want to 'clean' the URL of the page the user is on (i.e. remove the query strings) because if there are any query strings when they change locations it can mess things up when they try and use filters.

I've found a way to get a clean URL

$url=strtok($_SERVER["REQUEST_URI"],'?');

And I thought I might be able to use

wp_redirect($url);

To refresh the page with a clean URL - but doesn't seem to work.

I think the reason this question is unique is it is related to a function that is started by a $_POST request, rather than hooked to an action which is being loaded. As a result, the page isn't fully reloaded. I have looked at other questions which tell me how to remove the query string (which is helpful enough) but not redirect me at the end.

Any thoughts much appreciated.

Linz Darlington
  • 515
  • 1
  • 10
  • 25

3 Answers3

0

You could try

header("Location: ".$url);
Thomas George
  • 76
  • 1
  • 6
  • Here are a few types of PHP redirects you can try (some you may need AJAX to use). https://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php – Thomas George Jul 21 '17 at 18:24
0

Please try it

$url = strtok((empty($_SERVER['HTTPS']) ? 'http://' : 'https://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], '?');
mokamoto12
  • 383
  • 1
  • 6
0

I ended up using Javascript to refresh the page, with a function that removed the query string:

<form method="post" onsubmit="setTimeout(function () { window.location = window.location.pathname; }, 150)">
    <input type="submit" name="switchman" id="switchman" value="Switch to Manchester" />
</form>
Linz Darlington
  • 515
  • 1
  • 10
  • 25