19
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【WordPress】 is_mobile()でスマホとタブレットを分ける方法

Last updated at Posted at 2014-07-11

is_mobileは、スマホとタブレットという判定をするので、タブレットはPCと同じがいいと言う場合に困る。

そんな時は、functions.phpに以下のコードを追記する。

functions.php
function is_mobile() {
	$useragents = array(
		'iPhone',          // iPhone
		'iPod',            // iPod touch
		'^(?=.*Android)(?=.*Mobile)', // 1.5+ Android
		'dream',           // Pre 1.5 Android
		'CUPCAKE',         // 1.5+ Android
		'blackberry9500',  // Storm
		'blackberry9530',  // Storm
		'blackberry9520',  // Storm v2
		'blackberry9550',  // Storm v2
		'blackberry9800',  // Torch
		'webOS',           // Palm Pre Experimental
		'incognito',       // Other iPhone browser
		'webmate'          // Other iPhone browser
	);
	$pattern = '/'.implode('|', $useragents).'/i';
	return preg_match($pattern, $_SERVER['HTTP_USER_AGENT']);
}

これでis_homeがtureの時はスマホ、falseの時はタブレットとPCという別け方ができる。

if(is_mobile()){
	//スマホの時
}else{
	//タブレット or PCの時
}
19
19
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?