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.

I built a mobile version of a wordpress site and added some PHP code in the wordpress index.php file to detect mobile users and HTTP-redirects them to m.website.com (which is not a wordpress installation. This PHP standalone mobile site scrapes the wp site for posts and displays them). All works well, and every request to the full site goes to m.website.com instead.

The only problem is that my team leader wants me to redirect website.com/2011/04/13/post-title to m.website.com/2011/04/13/post-title for mobile users. And to do this, my redirection script only needs to know the permalink of the page that index.php is trying to route to. The mobile site's index.php will handle the permalink once it receives it.

N.B. Currently, if you visit m.website.com/2011/04/13/post-title it displays properly. I only need to know how to trap the permalink from inside wordpress and feed it to my mobile redirection code.

EDIT:

Thanks to Tom J Nowell's comment below, I am ready to move the redirection script to whatever file it is at which Wordpress figures out the permalink, and not index.php any longer. So that once the permalink is known, a redirection to m.website.com/router.php?path=2011/04/13/post-title can be done, which becomes m.website.com/2011/04/13/post-title via htaccess. Thanks

share|improve this question
    
So you want to get the permalink before the file figures out the permalink? ( the process of 'routing' as you call it, is an integral part of figuring out what the current url or permalink is ), as such your question is contradictory –  Tom J Nowell Apr 13 '12 at 14:07
    
Thanks. Fair enough, I can move my redirection script to the point at which the routing is done, and then redirect from there. At what point is the permalink figured out, and what file should I inspect, please? –  Cogicero Apr 13 '12 at 14:11
add comment

1 Answer

up vote 0 down vote accepted

I'd use responsive design rather than a mobile subdomain copy of the site, however if you really do want this, then put this code in a plugin:

// this function doesn't exist prior to wordpress v3.4
if(!function_exists('wp_is_mobile')){
   /**
    * Test if the current browser runs on a mobile device (smart phone, tablet, etc.)
    *
    * @return bool true|false
    */
   function wp_is_mobile() {
       static $is_mobile;

       if ( isset($is_mobile) )
           return $is_mobile;

       if ( empty($_SERVER['HTTP_USER_AGENT']) ) {
           $is_mobile = false;
       } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.)
           || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false
           || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false
           || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false ) {
               $is_mobile = true;
       } else {
           $is_mobile = false;
       }

       return $is_mobile;
   }
}
add_action('init','mobile_redirect_init');
function mobile_redirect_init(){
    if(wp_is_mobile()){
        $url = 'http://m.'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];;
        wp_redirect($url);
    }
}
share|improve this answer
    
(No, I am under instructions not to use a responsive design) Thanks! I'll try this. I already have code to detect mobile, just like your wp_is_mobile above. Can I place the add_action and the mobile_redirect_init() inside the theme functions.php file instead of making it a plugin? –  Cogicero Apr 13 '12 at 14:24
    
Also can get_permalink work for the current page? Cos get_permalink says Note that when used outside The Loop on a posts page (index, archive, etc.) without the ID parameter, it will return the URL of the last post in The Loop, not the permalink for the current page. See: core.trac.wordpress.org/ticket/9963 –  Cogicero Apr 13 '12 at 14:29
    
Why would anybody want you not to use a responsive design? hmmmm, I've updated my answer –  Tom J Nowell Apr 13 '12 at 14:42
    
Corporate bureaucracy. The client did not pay for a responsive design, and wants a standalone mobile site instead. It's quite complicated, thanks to my Team Leader. Sigh. Thanks for the updated code. Can I use this in the index.php page then? Or functions page? Or where? I am not allowed to install plugins (for the same bureaucratic reason). –  Cogicero Apr 13 '12 at 14:46
1  
Thanks Tom. (Actually the main site already existed. We're only building the mobile one). You gave me so many pointers and took valuable time, so I will accept your answer. But now that I have realized I dont need permalinks anyway, what I did was $url = 'http://m.'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]; and then I checked if it is mobile, and redirected to that URL if so. Worked fine. –  Cogicero Apr 13 '12 at 15:33
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.