A
A
Aligatro2014-02-21 18:37:50
PHP
Aligatro, 2014-02-21 18:37:50

How to implement a redirect to the mobile version of the site in Wordpress?

Good day, dear community. I'm here in an attempt to solve my non-trivial task - I have reached a dead end. In general, I adapted my site for mobile devices and added a switch between versions, of this kind.

/*-- Переключение на мобильную версию сайта ссылкой --*/
 session_start(); // начинаем сессию
 if (isset($_REQUEST['version'])) { // перехватываем линк
        $_SESSION['version'] = $_REQUEST['version'];
}
function mobileview($on){ // создаем удобную функцию
$on=$_SESSION['version'] == 'mobile';
return $on;
}

Everything works great, but I would like to make it so that the first time a user with a mobile browser enters the site, it is forced to drop to the pda version (but only 1 time, so that later I can switch to the desktop session). In general, the question itself is: how to do it?
Ps I tried to make a deck through the stump (cookies and other obscurantism):
if (wp_is_mobile()) {
        setcookie('mobile', '1', time()+9999999, '/', home_url());
        header('Location:' . home_url() . '/?version=mobile');
}

if ($_COOKIE['mobile'] != '1') {
   if ($_SESSION['version'] == 'desktop') {
    setcookie('mobile', '2', time()+9999999, '/', home_url());
    header('Location:' . home_url());
   }
}

But I realized that I am reinventing the wheel and this can be solved much easier. And the above script unfortunately did not work.
Many thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Aligatro, 2014-02-25
@Aligatro

The problem is nevertheless solved =) As expected, the solution turned out to be outrageously simple.

if( !isset($_SESSION['version']) && wp_is_mobile()) {
   wp_redirect( '/?version=mobile' ); 
exit;
}

wp_redirect can be replaced with a banal header location, but for some reason wordpress does not adequately recognize it.
Ps By the way, another question arose, in order to determine whether the session is open, what is better to use? isset, empty or $_SESSION['version'] == '' ? It's just that someone says isset is more correct, others say this method is not processed by old browsers, in general I'm confused =))
Thanks for the help =)

C
Codebaker, 2014-02-21
@Codebaker

I am a mobile device user, I beg you: do not make a mobile version of the site! She is not needed. Already today there are mobile devices with a resolution MORE than in work computers. And waiting for the mobile version to load first, and then the full version of the site in the conditions of greedily slow cellular networks is such a torment that you can’t pick up normal words right away (hello to the big three!). Thank you very much for your understanding!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question