Skip to content
Advertisement

Get current URL with PHP and change a part of it for a new url

I am working on a WordPress website for a client. I want to create a switch element through a current URL change. For instance;

https://mywebsite.com/shop/CUSTOMER should change to https://mywebsite.com/shop/OWNER

  echo $ _SERVER ['REQUEST_URI']; ?>

I have found a php code that provides me the URL of the current page. So if the visitor is on the /shop/customer page, I’ve found a way to create a shortcode and add my own string to it. But it creates a new link like; /shop/customer/owner

I want to replace customer in owner, not adding owner to the current link. Because it has to be a dynamic solution. For example, if the visitor is on https://mywebsite.com/shoppingcart/customer, they should change this to https://mywebsite.com/shoppingcart/owner

Sorry for the bad english, hope you understand what i need.

Regards, Rick

Advertisement

Answer

May be this could work!

$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$change_url = str_replace("customer","shopping",$actual_link);
echo $change_url;
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement