create array from existing URL insert array into on page link url if array does not exist insert default values into on page href link instead

StackOverflow https://stackoverflow.com/questions/12487167

  •  02-07-2021
  •  | 
  •  

Question

I am using PHP shortcode exec in wordpress to execute PHP in posts and pages

I want to take variables from the URL and instert them into the href URL links on the page but if no variables exist in the URL I want to instead insert default values.

E.G. if url has variables like http://mysite.com/?var1=123&var2=456&var3=789

url on page should be href="http://othersite.com/?var1=123&var2=456&var3=789

If the URL is: http://mysite.com/ links on site should be href="http://othersite.com/?var1=abc&var2=def&var3=ghi

I am new to PHP so I am sorry I don't have broken code for you to fix I am starting from scratch.

Thanks!!

No correct solution

OTHER TIPS

$_GET contains the parameters sent.

So the following (untested) will give you an idea.

// Set your default values
$aValues = array(
    'var1' => 12,
    'var2' => 12,
    'var3' => 12
      );

// Now run through the default values to see if they were sent with $_GET
foreach ($aValues as $key=>$value) {
    // Isset does the check to see if they were sent
    if (isset($_GET[$key])) {
        $aValues[$key] = $_GET[$key];
    }
}

$url = 'http://mysite.com/?' . http_build_query($aValues); 
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') 
                === FALSE ? 'http' : 'https';
$host     = $_SERVER['HTTP_HOST'];
$script   = $_SERVER['SCRIPT_NAME'];
$params   = $_SERVER['QUERY_STRING'];

$currentUrl = $protocol . '://' . $host . $script . '?' . $params;


if(isset($_GET["var"])) {
 echo $currentUrl; 
}
else { echo "yourwebsite with defualt vars" }

Sets Current url to the $currentUrl Variable. If statment checks if var is set if it is, echo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top