Detecting mobile or tablet device

前端 未结 3 848
迷失自我
迷失自我 2021-01-06 06:42

The aim for me is to have a mobile website (for mobiles and tablets) and a responsive desktop website, built on Wordpress. I want the easiest way to implement fool proof dev

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-06 07:10

    For Mobile Detection on PHP I have always used the $_SERVER['HTTP_USER_AGENT'] variable. I use the function below to have very granular control over who gets delivered the mobile site (specific devices I want to support). Simply add useragents to the array (such as 'Ipad') to add additional devices.

    function detectmobile(){
        $agent = $_SERVER['HTTP_USER_AGENT'];
        $useragents = array (
            "iPhone",
            "iPod",
            "Android",
            "blackberry9500",
            "blackberry9530",
            "blackberry9520",
            "blackberry9550",
            "blackberry9800",
            "webOS"
            );
            $result = false;
        foreach ( $useragents as $useragent ) {
        if (preg_match("/".$useragent."/i",$agent)){
                $result = true;
            }
        }
    return $result;
    }
    if (detectmobile() == true) { wp_redirect('http://pageyouwwanttoredirectto'); }
    

    Usage: you can the above code to your wordpress theme's functions.php file.

提交回复
热议问题