How can I redirect mobile users to diff url, but also allow them to come back to full site

function mobile_redirect() {
    if (isset($_COOKIE["redirected"]) && $_COOKIE["redirected"])
        return;
    global $is_iphone;
    if( isset($is_iphone) && $is_iphone ) {
        wp_redirect( '/mobile/' );
        setcookie("redirected", true);
        exit;
    }
}
add_action('init', 'mobile_redirect');

This code redirects every mobile visitor to /mobile/ and then sets a cookie that the user is redirected. If the cookie is set, then the redirect doesn’t take place. So if the user again goes to your site it won’t redirect to the mobile url.

You can also set the cookie if there is a variable in the url, like /?v=desktop, then if that url is visited it sets the cookie and doesn’t redirect anymore.


UPDATE

Use of builtin $is_iphone variable;
Thx to Kaiser.

Btw: strange name for a variable that’s used for every mobile device. $is_mobile would be a better name.

tech