Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

this is pretty straight forward: How do I redirect mobile users to /mobile, but still allow them to click a "Full site" button and see the full site without being redirected again back to the mobile site?

I have played with a bunch of the plugins that say they do this, but none seem to handle the second step. They redirect to the /mobile . but if a user wants to go to the full site, it tries then redirects back to mobile.

I am running WP3.0.1

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted
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.

share|improve this answer
1  
WP has already the built in global $is_iphone; that triggers for all sorts of mobile devices. No need to again check with a slow preg_match for that. Upvote anyway. –  kaiser Jan 16 '12 at 17:49
1  
Thx Kaiser, updated my answer. –  Rob Vermeer Jan 16 '12 at 18:49
add comment

I agree with Shaun. If you design your current theme well, you can just make it so that mobile devices use a different stylesheet. So basically you'll only need one theme, just two different style variants.

Example code used for changing stylesheets based on desktop or mobile device site access:

// Function in functions.php file

function is_mobile_device(){
    $device = '';

    if( stristr($_SERVER['HTTP_USER_AGENT'],'ipad') ) {
        $device = "ipad";
    } else if( stristr($_SERVER['HTTP_USER_AGENT'],'iphone') || strstr($_SERVER['HTTP_USER_AGENT'],'iphone') ) {
        $device = "iphone";
    } else if( stristr($_SERVER['HTTP_USER_AGENT'],'blackberry') ) {
        $device = "blackberry";
    } else if( stristr($_SERVER['HTTP_USER_AGENT'],'android') ) {
        $device = "android";
    }

    if( $device ) {
        return true; 
    } return false; {
        return false;
    }
}

 // Code in header.php

<?php if (is_mobile_device()) { 
      echo 'MOBILE STYLESHEET' 
   } else {
      echo 'DESKTOP STYLESHEET' }
?>
share|improve this answer
    
Agreed on the responsive design side. thanks –  TJ Sherrill Jan 15 '12 at 21:10
add comment

I'm using Obox Mobile on my website (http://clickwp.com) and it works really well even with W3 Total Cache. You can switch to the full site with no problem.

Although I agree that a responsive theme is probably the best way to go if you're looking for an easy solution. As you can see from my site, the sideshow gallery doesn't scale. A well-built responsive theme would take these into consideration as its not relying on a 3rd party plugin to provide the mobile functionality.

That being said, Obox Mobile is a good solution for me until I have the time and money to make my site responsive. Please use my affiliate link if you want to buy it, thanks!

http://www.obox-design.com/premium-wordpress-themes.cfm?afId=DB7FA9B3&bannerId=40

share|improve this answer
    
Agreed on the responsive design side. thanks –  TJ Sherrill Jan 15 '12 at 21:10
add comment

Check the $is_iphone; variable. Then do your own redirects.

share|improve this answer
    
This makes sense in general. I have had no problem forwarding the user to the mobile site, my main issue is when the mobile user wants to come back to the main site the redirect sends them back to the mob site. –  TJ Sherrill Jan 13 '12 at 21:30
    
Plugins won't solve your edge case task. As I said: "do your own redirects". I gave you the knife, now cook :) –  kaiser Jan 13 '12 at 21:38
1  
You could add a query var when returning from the mobile site, and check if it exists before the redirect starts. –  Mario Jan 13 '12 at 22:10
1  
Using responsive design and media queries you won't need to build a separate mobile site at all, and so won't need redirection. –  Shaun Jan 13 '12 at 23:22
1  
The var just triggers for every "mobile" device, not just the iPhone. The name is missleading... –  kaiser Jan 14 '12 at 15:31
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.