1

We're using wordpress on a publishing website and I have a situation where I need to use server-side mobile detection in order to deliver some ad scripts. Javascript is not a solution since the scripts from the ad server are pretty complex.

The problem is that the condition simply doesn't work. Desktop is delivered to mobile instead.

I tried 2 solutions so far:

wp_is_mobile() Mobile_Detect.php

Both of them are working locally - true on mobile, false on Desktop (using Mamp), but none works in the production server.

I tried enabling the Mobile Cache from the Litespeed Cache plugin, tried other recommended rewrite conditions from their documentation. Nothing Works.

We also tried a simple implementation of mobile_detect outside WordPress, same issue: desktop is delivered to mobile.

Our server is a VPS with WHM, so we have root access.

Any recommendation is welcomed!

Thanks!

  • 1
    if mobile cache on plugin didn't work , you may need to enable server debug log , to check what exactly was the rewrite rule interpreted and how was vary being added – qtwrk Jan 15 '21 at 15:31
  • 1
    Did you do purge cache after rewrite rule / Mobile Cache applied? if not, it will still service the desktop cache to mobile view – Eric Jan 18 '21 at 01:49

1 Answers1

0

Have you tried this?

function wp_is_mobile() {
    if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
        $is_mobile = false;
    } elseif ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Mobile' ) !== false // Many mobile devices (all iPhone, iPad, etc.)
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'Android' ) !== false
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'Silk/' ) !== false
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'Kindle' ) !== false
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'BlackBerry' ) !== false
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera Mini' ) !== false
        || strpos( $_SERVER['HTTP_USER_AGENT'], 'Opera Mobi' ) !== false ) {
            $is_mobile = true;
    } else {
        $is_mobile = false;
    }
 
    /**
     * Filters whether the request should be treated as coming from a mobile device or not.
     *
     * @since 4.9.0
     *
     * @param bool $is_mobile Whether the request is from a mobile device or not.
     */
    return apply_filters( 'wp_is_mobile', $is_mobile );
}

credits: https://developer.wordpress.org/reference/functions/wp_is_mobile/

randomdude
  • 267
  • 2
  • 11