I'm running a WordPress site and wanna setup a mobile site based on it.

I've tried some plugins like Mobile Themes which allows me to implement a different theme for mobile. The problem is when I use memcached, the main theme and mobile theme is confusing very often(e.g. access the site by mobile but meet a main theme)

I'm considering another way to do this now. Use a PHP mobile detective script, and write the code in all theme template files(e.g. header.php) like:

if(is_mobile()) {
  // mobile theme code
} else {
  // main theme code

for this, I need only to maintain one theme. I've never tried this way, not sure if there is any problem, and I didn't think about how to handle the style.css yet.(There are some plugins allow me to use different style rules for mobile, but I've never tried)

Does anybody could provide some suggestions?

  • 3
    There is no reliable way to do this with php. wp_is_mobile() is a joke and very unreliable and will fail in many enviroments. CSS responsive design is your best option here and much more reliable than php in this case. For extra info, read this answer I've done recently on something similar Jul 20 '15 at 7:00
  • 1
    wp_is_mobile() The codex says it should not be used for themes. There must be some reliability issues. You better off using css media queries.
    – Sisir
    Jul 20 '15 at 7:02
  • @PieterGoosen @Sisir Thanks for reply! I will not consider responsive theme because of the limitation of design and my site is a little complex, including many modules which shouldn't appear on mobile(use display:none is not a solution). And I'm not talking about wp_is_mobile(), I found some PHP scripts or plugins can do the check. Do you have any idea about if they are reliable? Or is there a mobile theme plugin which can be compatible with Memcached, eAccelerator and W3 Total Cache?
    – Brick Yang
    Jul 20 '15 at 7:08
  • All php scripts that handles device detection in unreliable and will fail. You will dish up wrong info to at least 10% of users, maybe even more. Jul 20 '15 at 7:16
  • @PieterGoosen Got it! So is there any way to detect device reliably?
    – Brick Yang
    Jul 20 '15 at 7:19

In my experience browscap.ini based checks are not terrible - at least. There is - probably not the only one - an PHP implementation with browsecap-php that can be used for PHP projects and would be suitable for WP projects too. As for a WordPress implementation, there is the plug-in - I know of - PHP Browser Detection, which as far as I can tell does a good job implementing the functionality. Last but not least, I can't give you any numbers on reliability though, so lets just say, seeing the discussion under your question, you have been warned.

  • Thanks. Do you have any idea about using the browser detection plugin to mix two themes in one? If the plugin is reliable, is this a reasonable way?
    – Brick Yang
    Jul 20 '15 at 8:17
  • The problem is that, even if the detection script is working perfectly, it is not 100% reliable, because browser can lie about their identity and there is absolutely nothing you can do about it. I don't exactly know what you mean by mixing themes, generally, either you make your theme show differently for devices based on CSS or if that is not enough you separate your mobile and desktop site, having two themes. You can of course try to add a specific class to e.g. the body by mobile/browser detection and style via CSS. But as said this can't 100% reliable - never ever ever... @BrickYang
    – Nicolai
    Jul 20 '15 at 8:35
  • Thanks. My question here is if the detection is not a problem(assume it's 100% reliable), dose the code like if(is_mobile()) {// mobile theme} else {// desktop theme} is a good solution?(I mean in all template files do check the device then output different HTML. This is one theme not two separate themes)
    – Brick Yang
    Jul 20 '15 at 9:03
  • I use wp_is_mobile() in some cases, and it works. Of course, you'll get some misses, but after digging through lots of GA stats and testing I'm pretty confident, that normal browsers without any tweaks and hacks are pretty reliable in terms of determining them - Safari, Chrome, Firefox, IE, Opera, some less popular like Yandex etc, both desktop and mobile. So if you have some responsive CSS + removing unnecessary blocks with wp_is_mobile(), you should be fine in most cases. Some 1-2% of misses is not worth your attention unless you are Amazon store. Jul 20 '15 at 11:53

Add your CSS rules to a Media Query at the end of your style sheet.

@media only screen and (max-width: 768px) {

        // Your CSS Rules

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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