• Resolved TiiuK

    (@tiiuk)


    Hi there,

    I have a responsive website but for some pages I would like to have two versions (desktop version & mobile version with different URL’s) and direct mobile users to the mobile URL when they click the link to that page in the menu.

    I’ve found plugins that do that but I would like to know what’s the best way to do it without a plugin.

    Kind regards,
    TiiuK

Viewing 5 replies - 1 through 5 (of 5 total)
  • What’s the reason for not wanting a plugin? Any solution would involve writing a plugin, so you might as well go with an existing solution.

    I will say though that the whole point of ‘responsive’ design is to have a website that works on all devices automatically, without the need for separate websites. Having a separate mobile and desktop site is generally considered an outdated approach.

    Is there something specific you’re trying to achieve that would require separate versions of the page?

    I totally agree with @jakept, but also think it’s right to answer, then you will do what’s better for you.

    You can try this code:

    function my_page_redirect(){
    	global $post;
    	if( !is_object( $post ) ) return;
    	$pages = array( $id1,$id2,....,$idn );
        if( in_array( $post->ID,$pages ){
    		$url = get_permalink( $post->ID );
    		//now you should manipulate the url to transform it in the mobile url
    		//it depends if you want a subdomain or another page in the same domain ....
    		//if e.g. the mobile url could be e.g. $url.'-mobile', in this case you should add:
    		//$url = $url.'-mobile'
            wp_redirect( $url );
            die;
        }
    }
    add_action( 'template_redirect', 'my_page_redirect' );

    You should replace $id1, $id2 … with the page IDs you want as mobile version.
    I’ve never tested this code, I’m not 100% that the global variable $post is available at this point, but is should, if it’s available it should work. Read the code comments

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Moved to Fixing WordPress, this is not a Developing with WordPress topic.

    Thread Starter TiiuK

    (@tiiuk)

    Thank you very much Jose. I was expecting to do it with .htaccess, I didn’t even thing of a JavaScript solution. Hm. The reason I don’t want to use a plugin is that it seems like an overkill for such a tiny thing … correct me if I am wrong.

    you are welcome. The proposed solution is a PHP solution, not a javascript solution, maybe you wanted to say PHP … About the use of a plugin, it always depend about your skills, what you have to do and how heavy is the plugin that should do what you wanted to do. I mean, people who are not familiar with code have no option, they have to install a plugin, this is not your case as I understand. For other cases if you solve the problem with few lines of code, of course it’s better using your code, because usually a plugin has to predict different scenarios … and its code would be probably a lot more than needed. But there are situations where the code you would write would be more and probably less optimized than a code of a known plugin, especially when the task is complicated. Consider that the plugins authors improve their plugins every time a new issue is found ….
    The problem of having many plugins is that usually they are loaded on each pages, but you could avoid this problem with Freesoul Deactivate Plugins. Sorry this is self promotion, but this is exactly the plugin needed by who doesn’t like plugins as you 🙂
    In any case better to have a full responsive website than redirect people to the mobile versions. The redirect needs time, a full responsive design is better also in terms of SEO. Full responsive I mean also that the image sizes are lower for lower screen viewports.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Redirect single site to mobile version without plugin’ is closed to new replies.