php - Appending URL parameter at every request only if parameter already exists to save opt-out option responsive design
I have a responsive Wordpress site: www.2eenheid.de. My client wants an option on a mobile to view the site in fullsize, an opt-out responsive option. Now, I found this solution:
http://css-tricks.com/user-opt-out-responsive-design/
Which uses PHP to remove themeta name="viewport"
tag when the URL parameter?resp=no
(URL becomes: www.2eenheid.de/?resp=no) is called. By removing themeta name="viewport"
tag the site becomes fullsize on mobile. See code below:
<head>
<title>My cool site huzzah</title>
<?php
if (!$_GET['resp'] == 'no') { ?>
<meta name="viewport" content="width=device-width">
<?php } ?>
</head>
<body class="<?php if (!$_GET['resp'] == 'no') { echo "resp"; } ?>">
This changes the current page to fullsize, but if I click a different link it changes back to responsive, which is logical because the link doesn't contain the parameter?resp=no
anymore.
Now my client has the requirement that if a user wants the fullsize website, it needs to stay fullsize even after clicking a different URL until a user changes it back to responsive.
So my question is how can i save the?resp=no
parameter once clicked for every URL request, until a user changes it back to responsive by e.g. clicking a different link (maybe with a different parameter)?
I've tried googling but I can't find any good solutions. I've seen people suggesting session values but I find that very hard to understand and some people say that thats not good practice because session values are used for logins.
Any help would be highly appreciated.
EDIT 3: The suggestion below seems good, but I'm quite new to PHP sessions. I changed my code with the suggestion below to this:
<?php ini_set('display_errors', true);
session_start();
if(isset($_REQUEST['resp'])) {
$_SESSION['resp'] = boolval($_REQUEST['resp']);
}
// Check if enabled
$enabled = isset($_SESSION['resp']) && $_SESSION['resp'];
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<?php if($enabled): ?>
<meta name="viewport" content="width=device-width">
<?php endif; ?>
<title>LALALA</title>
</head>
<body <?php body_class(); ?> id="<?php if(isset($_SESSION['resp']) && $_SESSION['resp']) { echo "resp"; } ?>">
Can anyone help with this?
Answer
Solution:
And :
You could probably use cookies too as said in a comment, but i never looked how it works.
EDIT : An example :
Answer
Solution:
Usings sessions for this is too much, a better mechanish exists, called "output_add_rewrite_var". Using this system, we can say the following:
The above code fits perfectly in the view place of a MVC system, since it is a view specific option. The code works by looking if
$_GET['resp']
exists or not, and depending on that, if it exists, callingoutput_add_rewrite_var("resp", "1");
to put itself in other url's, and if it doesn't exists, it adds the meta tag.