0

I'm trying to integrate the PHP version of the mobile detection script from http://mobiledetect.net/ into my Wordpress installation.

I've used the following code to call and detect mobile usage

<?php
    include 'includes/Mobile_Detect.php';
    $detect = new Mobile_Detect;
    $isMobile = $detect->isMobile();
    if($isMobile) {
        //Do a thing;
    };
?>

While the code does work, I've found that the loading time of the page has risen by 1-2 seconds on a consistent basis. I've run some tests and just the include statement itself is causing a major slowdown.

Is this normal when using this script or is there supposed to be a better way to implement it?

  • find another faster script? what can we do here? –  Dec 07 '17 at 00:11
  • Use the wordpress built in [function](https://codex.wordpress.org/Function_Reference/wp_is_mobile)? What's the thing you want to do? I'll be surprised if php is the best way of doing it... – miknik Dec 07 '17 at 00:13

3 Answers3

0

The script you have included might be doing some cpu intensive calculations (which might be slowing it down), the only way is to find better script, OR If you want you can write your own code in PHP of 5-6 lines that can detect if user is on mobile. You can get more details on that here is this answer : Mobile device detection in PHP

zsubzwary
  • 846
  • 7
  • 20
0

WordPress have a built-in function wp_is_mobile() to check if users using mobile or not. So you don't have to include and using unnessacry external script or function.

Argus Duong
  • 1,638
  • 1
  • 8
  • 23
  • Do NOT use that function (it doesn't do what you think it does). See here:https://wordpress.stackexchange.com/questions/188881/proper-usage-of-wp-is-mobile – Dejo Dekic Dec 07 '17 at 14:59
  • I know but that question is 2 years old and now they [updated](https://codex.wordpress.org/Function_Reference/wp_is_mobile) with **This is perfectly fine to use in themes** and I think it fit with this answer needs. – Argus Duong Dec 08 '17 at 03:33
0
if ( wp_is_mobile() ) 
{
    //Do a thing;
}
else
{
    //Do a thing;
}
Tarang koradiya
  • 662
  • 5
  • 7