{"id":729,"date":"2019-12-17T21:36:50","date_gmt":"2019-12-17T21:36:50","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/12\/17\/check-if-current-area-is-frontend-or-backend-in-magento-2\/"},"modified":"2025-05-22T15:12:55","modified_gmt":"2025-05-22T09:42:55","slug":"check-if-current-area-is-frontend-or-backend-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/check-if-current-area-is-frontend-or-backend-in-magento-2\/","title":{"rendered":"How To Check If The Current Area Is Frontend Or Backend In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The Magento 2 store admin gets additional privileges for efficiently fulfilling their responsibility as a store admin.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the admin gets the facility to use a particular shipping or payment method while placing an order from the backend.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, to implement admin-exclusive features, you need to first <em><strong>check if the current area is frontend or backend in Magento 2<\/strong><\/em>. The method given below allows checking if it is frontend or backend.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Check the area of the <a href=\"https:\/\/meetanshi.com\/blog\/get-current-url-in-magento-2\/\">current URL in Magento 2<\/a> with any of the two methods:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">1. With objectManager<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">2. With class method<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And, you get one of these outputs as frontend, adminhtml, or webapi_rest.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to check if the current area is frontend or backend in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. <strong>With objectManager<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\n$state =  $objectManager->get('Magento\\Framework\\App\\State');\necho $state->getAreaCode();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. <strong>With class method<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nnamespace Vendor\\Extension\\Block;\nuse Magento\\Framework\\App\\State;\nuse Magento\\Framework\\View\\Element\\Template;\nuse Magento\\Framework\\View\\Element\\Template\\Context;\nclass Index extends Template\n{\n    protected $state;\n    public function __construct(Context $context, State $state array $data = [])\n    {\n        parent::__construct($context, $data);\n        $this->state = $state;\n    }\n    public function getArea()\n    {\n        return $this->state->getAreaCode();\n    }\n}\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Any doubts?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do share the solution with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Magento 2 store admin gets additional privileges for efficiently fulfilling their responsibility as a store admin. For example, the admin gets the facility to&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-729","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=729"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/729\/revisions"}],"predecessor-version":[{"id":15146,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/729\/revisions\/15146"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}