{"id":306812,"date":"2021-09-23T18:53:53","date_gmt":"2021-09-23T18:53:53","guid":{"rendered":"https:\/\/webkul.com\/blog\/?p=306812"},"modified":"2026-02-27T07:56:08","modified_gmt":"2026-02-27T07:56:08","slug":"detect-your-device-is-mobile-or-desktop-in-magento-2","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/","title":{"rendered":"Detect your device is Mobile or Desktop in Magento 2"},"content":{"rendered":"\n<p>This blog explains how you can detect whether a user is accessing your store from a mobile or desktop device in Magento 2.<\/p>\n\n\n\n<p>It helps you customize layouts, features, and performance to deliver a better user experience on every device.<\/p>\n\n\n\n<p>In Magento 2, if you want to display different content or layout on a page according to Desktop or Mobile, then you use the following code in your code.<br><br><strong>1.<\/strong> <strong>To Detect Desktop Device:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">$userAgent = $this-&gt;getRequest()-&gt;getHeader(&#039;useragent&#039;);\n$server    = $this-&gt;getRequest()-&gt;getServer();\n        \n$isDesktopDevice = \\Zend_Http_UserAgent_Desktop::match($userAgent, $server);\nif ($isDesktopDevice) {\n   \/\/Write your code here for Desktop view\n}<\/pre>\n\n\n\n<p><strong>2. To Detect Mobile Device:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">$userAgent = $this-&gt;getRequest()-&gt;getHeader(&#039;useragent&#039;);\n$server    = $this-&gt;getRequest()-&gt;getServer();\n\n$isMobileDevice = \\Zend_Http_UserAgent_Mobile::match($userAgent, $server);\nif ($isMobileDevice) {\n   \/\/Write your code here for Mobile view\n}<\/pre>\n\n\n\n<p>Here, check the complete code in a controller file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\">&lt;?php\n\/**\n * Vendor&#039;s Short Description\n *\n * @category  Vendor\n * @package   Vendor_CustomModule\n * @author    Vendor\n * @copyright Copyright (c) Vendor\n * @license   https:\/\/example.com\/license.html\n *\/\nnamespace Vendor\\CustomModule\\Controller\\Demo;\n\nuse Magento\\Framework\\App\\Action\\Action;\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\View\\Result\\PageFactory;\n\nclass CheckDevice extends Action\n{\n    \/**\n     * @var PageFactory\n     *\/\n    protected $_resultPageFactory;\n\n    \/**\n     * initialization\n     *\n     * @param Context $context\n     * @param PageFactory $resultPageFactory\n     *\/\n    public function __construct(\n        Context $context,\n        PageFactory $resultPageFactory\n    ) {\n        $this-&gt;_resultPageFactory = $resultPageFactory;\n        parent::__construct($context);\n    }\n    \n    \/**\n     * Execute method\n     *\/\n    public function execute()\n    {\n        $resultPage = $this-&gt;_resultPageFactory-&gt;create();\n        $userAgent  = $this-&gt;getRequest()-&gt;getHeader(&#039;useragent&#039;);\n        $server     = $this-&gt;getRequest()-&gt;getServer();\n         \n        \/\/check is device is Mobile\n        $isMobileDevice = \\Zend_Http_UserAgent_Mobile::match($userAgent, $server);\n        if ($isMobileDevice) {\n            $resultPage-&gt;getConfig()-&gt;getTitle()-&gt;set(__(&quot;View For Mobile&quot;));\n        }\n\n        \/\/check is device is Desktop\n        $isDesktopDevice = \\Zend_Http_UserAgent_Desktop::match($userAgent, $server);\n        if ($isDesktopDevice) {\n            $resultPage-&gt;getConfig()-&gt;getTitle()-&gt;set(__(&quot;View For Desktop&quot;));\n        }\n        return $resultPage;\n    }\n}<\/pre>\n\n\n\n<p><strong>Note:<\/strong> In the latest version of Magento, you can face <em>Zend_Http_UserAgent_Mobile is deprecated <\/em>error. So, for Magento&#8217;s latest version, please refer to the following blog:<br><a href=\"https:\/\/webkul.com\/blog\/how-to-detect-the-mobile-devices-in-the-latest-magento-as-zend_http_useragent_mobile-is-deprecated\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/webkul.com\/blog\/how-to-detect-the-mobile-devices-in-the-latest-magento-as-zend_http_useragent_mobile-is-deprecated\/<\/a><\/p>\n\n\n\n<p>Hope this will be helpful. Thanks \ud83d\ude42<br><br><strong>Previous Blog:<\/strong> <a href=\"https:\/\/webkul.com\/blog\/reindexing-for-one-product-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Reindexing for one product in Magento 2<\/a><br><br><strong>Next Blog:<\/strong> <a href=\"https:\/\/webkul.com\/blog\/override-product-special-price-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">Override Product Special Price in Magento 2<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This blog explains how you can detect whether a user is accessing your store from a mobile or desktop device in Magento 2. It helps you customize layouts, features, and performance to deliver a better user experience on every device. In Magento 2, if you want to display different content or layout on a page <a href=\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":249,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9121],"tags":[12096,12095,12094,12093,4155,12097,12098],"class_list":["post-306812","post","type-post","status-publish","format-standard","hentry","category-magento-2","tag-desktop","tag-detect","tag-detect-device","tag-device","tag-mobile","tag-useragent","tag-zend-http"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Detect your device is Mobile or Desktop in Magento 2<\/title>\n<meta name=\"description\" content=\"This blog explains how you can detect whether a user is accessing your store from a mobile or desktop device in Magento 2.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Detect your device is Mobile or Desktop in Magento 2\" \/>\n<meta property=\"og:description\" content=\"This blog explains how you can detect whether a user is accessing your store from a mobile or desktop device in Magento 2.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-23T18:53:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-02-27T07:56:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Khushboo Sahu\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Khushboo Sahu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/\"},\"author\":{\"name\":\"Khushboo Sahu\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\"},\"headline\":\"Detect your device is Mobile or Desktop in Magento 2\",\"datePublished\":\"2021-09-23T18:53:53+00:00\",\"dateModified\":\"2026-02-27T07:56:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/\"},\"wordCount\":151,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"keywords\":[\"desktop\",\"detect\",\"detect device\",\"device\",\"mobile\",\"useragent\",\"zend http\"],\"articleSection\":[\"Magento 2\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/\",\"url\":\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/\",\"name\":\"Detect your device is Mobile or Desktop in Magento 2\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"datePublished\":\"2021-09-23T18:53:53+00:00\",\"dateModified\":\"2026-02-27T07:56:08+00:00\",\"description\":\"This blog explains how you can detect whether a user is accessing your store from a mobile or desktop device in Magento 2.\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Detect your device is Mobile or Desktop in Magento 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca\",\"name\":\"Khushboo Sahu\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g\",\"caption\":\"Khushboo Sahu\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Detect your device is Mobile or Desktop in Magento 2","description":"This blog explains how you can detect whether a user is accessing your store from a mobile or desktop device in Magento 2.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/","og_locale":"en_US","og_type":"article","og_title":"Detect your device is Mobile or Desktop in Magento 2","og_description":"This blog explains how you can detect whether a user is accessing your store from a mobile or desktop device in Magento 2.","og_url":"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2021-09-23T18:53:53+00:00","article_modified_time":"2026-02-27T07:56:08+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-og.png","type":"image\/png"}],"author":"Khushboo Sahu","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Khushboo Sahu","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/"},"author":{"name":"Khushboo Sahu","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca"},"headline":"Detect your device is Mobile or Desktop in Magento 2","datePublished":"2021-09-23T18:53:53+00:00","dateModified":"2026-02-27T07:56:08+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/"},"wordCount":151,"commentCount":2,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"keywords":["desktop","detect","detect device","device","mobile","useragent","zend http"],"articleSection":["Magento 2"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/","url":"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/","name":"Detect your device is Mobile or Desktop in Magento 2","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"datePublished":"2021-09-23T18:53:53+00:00","dateModified":"2026-02-27T07:56:08+00:00","description":"This blog explains how you can detect whether a user is accessing your store from a mobile or desktop device in Magento 2.","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/detect-your-device-is-mobile-or-desktop-in-magento-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Detect your device is Mobile or Desktop in Magento 2"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/f94b8f53397bf85810761d76c98fadca","name":"Khushboo Sahu","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cabac965df656d114e6bf340df07518c990eda03bb09265dbd5c17f1097adaae?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Feva.png&r=g","caption":"Khushboo Sahu"},"url":"https:\/\/webkul.com\/blog\/author\/khushboo-sahu062\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/306812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/249"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=306812"}],"version-history":[{"count":5,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/306812\/revisions"}],"predecessor-version":[{"id":528545,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/306812\/revisions\/528545"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=306812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=306812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=306812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}