What is the correct way to do a mobile theme for WordPress?

I’m building a custom theme, along with a custom mobile theme counterpart. They don’t share resources, CSS, etc. (cause I like to keep them clean, and separate).

My question is: how does everyone (preferably other professionals) go about doing this? I’m using the Mobile Detect script to redirect users to the ‘mobile’ sub-folder when they access the site via their mobile phone, etc. Is there another, more elegant way of doing this?

Related posts

Leave a Reply

2 comments

  1. Will,

    You can use the template include filter to redirect visitors to a different page template conditionally.

    Example code to redirect a custom post type template:

    add_filter('template_include', 'get_customer_review_cpt_template', 100);
    
    function get_customer_review_cpt_template($template){
    global $template;
    
        // Our custom post type.
    $post_type   = 'customer_reviews';
    $post_object = $GLOBALS['post'];
    
    if ( !isset( $post_object->post_type ) ) {
        return $template;
    }
        // Send our plugin file.
    if ( is_singular() && $post_object->post_type === $post_type ) {
        return dirname(__FILE__) . "/views/single-$post_type.php";
    }
    return $template;
     }
    

    Additionally, you can also use the add_filter('template','get_your_new_theme'); to conditionally direct visitors to a different theme if they are on a mobile device. You are a little limited with what you can do here, but I have found it useful for some mobile sites.

    Here is some code I used as a plugin to direct mobile visitors to a different theme: https://gist.github.com/3454745