1

I've inherited a site which has a mobile version. Let's call it qqq.com. It has a Varnish server in front of it to cache the pages. But it seems the Varnish server is also detecting mobile requests, and changing the URL to m.qqq.com.

The WP install is then switching to the qqq-mobile theme, instead of the qqq theme.

How would you do that? I'm trying to locate the code that does this, so I can do the same thing on my local dev instance.

Thanks for your help.

1 Answer 1

5

The activated theme is stored in the options table: template is the parent theme, stylesheet is the child theme. If there is no child theme the two values will be the same.

The current hostname (URL without protocol or path) is available in the $_SERVER variable.

You can then hook into the stylesheet and template filters to force a different theme.

function use_mobile_theme( $current_theme ) {
    // If the domain is m.qqq.com and the current theme is 'qqq'
    if ( 'm.qqq.com' === $_SERVER['HTTP_HOST'] && 'qqq' === $current_theme ) {
        // Use the 'qqq-mobile' theme instead
        return 'qqq-mobile';
    } else {
        // Otherwise, keep the current theme
        return $current_theme;
    }
}

add_filter( 'stylesheet', 'use_mobile_theme' );
add_filter( 'template', 'use_mobile_theme' );

If qqq-mobile is a child theme, remove the add_filter( 'template', ... line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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