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'm trying to create mobile-friendly version for a wp site. I'm using the "any mobile theme switcher" to switch to an alternative (mobile) theme which works well.

I have also created some alternative key pages (home, contact & news) which are heavily edited (shorter) versions of the desktop theme content and added these to a mobile-only menu. Again - this is working well.

The problem I'm trying to solve is how to avoid someone using a mobile device landing on the desktop version of the home page?

I can't see a way of setting a different home / front page within my mobile theme files so..

Can mobile devices be re-directed to my alternative "mobile homepage" instead? And If at all possible, retain the option to view the full desktop version if desired.

I see the codex describes useful bits of code, but I need help to make them all work together. In human terms, I think what I need is something that does this -

If is home page, And if is mobile, Redirect to http://mysite.com/mobile-homepage

Many Thanks

share|improve this question

3 Answers 3

You can make a simple function for this, check if is mobile (http://codex.wordpress.org/Function_Reference/wp_is_mobile). and redirect with wp_redirect() http://codex.wordpress.org/Function_Reference/wp_redirect

add_action('wp_head', 'redirect_mobile');
function redirect_mobile()
{
   if ( wp_is_mobile() ) 
   {
       wp_redirect( 'http://url' ); 
       exit; 
   }
}

This is untested but should work. Add it to your themes functions.php file.

share|improve this answer
    
Thanks Pontus - Heading in the right direction I think. I added that to my mobile theme's functions.php, but Iphone can't complete - "too many redirects". Probably a clash with the theme swap plugin I'm using - I'll investigate further... –  speedypancake Jul 25 '13 at 17:38
    
... No luck I'm afraid. Uninstalled the plugin. Then tried moving your script over to the standard desktop theme instead, but safari's still freaking out about "too many redirects". It's only 1, isn't it?!! –  speedypancake Jul 25 '13 at 17:43
    
Think Pontus may have inspired me towards a practical solution. If anyone can see a problem with what I'm doing, or suggest a better way, please let me know - Thanks –  speedypancake Jul 25 '13 at 19:30

Similar but without redirection:

function template_choose() {

  $part = wp_is_mobile() ? 'mobile' : null;

  if( is_front_page() ) {

    get_template_part( 'home', $part );

    // if not mobile search (in this order) for
    // {$childthemepath}/home.php
    // {themepath}/home.php

    // if mobile search (in this order) for
    // {$childthemepath}/home-mobile.php
    // {$themepath}/home-mobile.php
    // {$childthemepath}/home.php
    // {$themepath}/home.php
    exit();

  } elseif ( is_page() ) { // same logic can be used with different Conditional Tags

    get_template_part( 'page', $part );
    exit();

  }

}
add_action( 'template_redirect', 'template_choose' );
share|improve this answer
    
Thanks for your answer - I'll try and find time to give it a go over the weekend if possible - Thanks –  speedypancake Jul 26 '13 at 13:48
up vote 0 down vote accepted

I cobbled together the following code from Pontus's answer and the answer to a slightly different problem here on WP Answers - This works alongside my mobile theme switching plugin, so when a mobile user visits the site, they are redirected to a new set of "mobile-friendly" pages and see my new responsive mobile theme, together with a collapsible mobile page menu :)

function so16165211_mobile_home_redirect(){
if( wp_is_mobile() && is_front_page() ){
   wp_redirect( '/mobile-home' ); 
    exit;
}
}
add_action( 'template_redirect', 'so16165211_mobile_home_redirect' );
share|improve this answer

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.