wpbones/useragent

检测用户代理的一个有用方法

1.0.2 2024-04-09 16:19 UTC

This package is auto-updated.

Last update: 2024-09-20 05:27:16 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License Monthly Downloads

检测用户代理的一个有用方法

需求

此包与使用 WP Bones 框架库 编写的 WordPress 插件兼容。

安装

您可以使用以下命令安装第三方包

php bones require wpbones/useragent

我建议使用此命令而不是 composer require,因为这样做会自动重命名。

您可以使用 composer 安装此包

composer require wpbones/useragent

您还可以将 "wpbones/useragent": "^1.0" 添加到您的插件的 composer.json 文件中

  "require": {
    "php": ">=7.4",
    "wpbones/wpbones": "~0.8",
    "wpbones/useragent": "~1.0"
  },

并运行

composer install

如何

您将能够使用 wpbones_user_agent() 函数获取 Mobile Detect 的实例。

if(wpbones_user_agent()->isMobile()) {
  echo "You're by Mobile";
} else {
  echo "You're by Desktop";
}

示例

// Basic detection.
wpbones_user_agent()->isMobile();
wpbones_user_agent()->isTablet();

// Magic methods.
wpbones_user_agent()->isIphone();
wpbones_user_agent()->isSamsung();
// [...]

// Alternative to magic methods.
wpbones_user_agent()->is('iphone');

// Find the version of component.
wpbones_user_agent()->version('Android');

您还可以

// Any mobile device (phones or tablets).
if ( wpbones_user_agent()->isMobile() ) {

}

// Any tablet device.
if( wpbones_user_agent()->isTablet() ){

}

// Exclude tablets.
if( wpbones_user_agent()->isMobile() && !wpbones_user_agent()->isTablet() ){

}

// Check for a specific platform with the help of the magic methods:
if( wpbones_user_agent()->isiOS() ){

}

if( wpbones_user_agent()->isAndroidOS() ){

}

// Alternative method is() for checking specific properties.
// WARNING: this method is in BETA, some keyword properties will change in the future.
wpbones_user_agent()->is('Chrome')
wpbones_user_agent()->is('iOS')
wpbones_user_agent()->is('UCBrowser')
wpbones_user_agent()->is('Opera')
// [...]