{"id":2184,"date":"2022-11-14T07:30:23","date_gmt":"2022-11-14T07:30:23","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/add-custom-phtml-file-in-magento-2-admin\/"},"modified":"2025-03-17T08:43:59","modified_gmt":"2025-03-17T08:43:59","slug":"add-custom-phtml-file-in-magento-2-admin","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-custom-phtml-file-in-magento-2-admin\/","title":{"rendered":"How to Add Custom Phtml File in Magento 2 Admin"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Want to&nbsp;<em><strong>add custom Phtml file in Magento 2 admin<\/strong><\/em>? Here\u2019s how you can do that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are a Magento developer working on&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-development-services.html\" target=\"_blank\" rel=\"noreferrer noopener\">custom development<\/a>, you may often require to change the layout of the admin panel for adding new features and functionalities. In&nbsp;<a href=\"https:\/\/business.adobe.com\/in\/products\/magento\/magento-commerce.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2<\/a>, Phtml files are used by the web servers to generate dynamic HTML files based on the PHP scripts and serve them to the clients, i.e. store admins &amp; customers. Therefore, in order to tweak anything that is displayed on the admin panel, we need to add a custom Phtml file in Magento 2 admin.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Earlier, one of my colleagues demonstrated a method to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/call-phtml-file-in-cms-static-block-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">call a Phtml file in CMS static block in Magento 2<\/a>. Today, I have come up with a method to insert a custom Phtml file in your Magento 2 admin panel through a module.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Custom Phtml File in Magento 2 Admin<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In order to add a custom Phtml file in Magento 2 admin layout, we\u2019ll be using the module method.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 1:<\/strong>&nbsp;First, we need to register a new module using the component registration class. Create&nbsp;<strong>app\/code\/Vendor\/Module\/registration.php&nbsp;<\/strong>file and add the following code (Feel free to replace the \u2018Vendor_Module\u2019 element):<\/li>\n<\/ul>\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\n\nuse Magento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(\n    ComponentRegistrar::MODULE,\n    'Vendor_Module',\n    __DIR__\n);<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 2:<\/strong>&nbsp;Now, add the basic information about the module by using the&nbsp;<strong>app\/code\/Vendor\/Module\/etc\/module.xml&nbsp;<\/strong>with the following code:<\/li>\n<\/ul>\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;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Module\" setup_version=\"1.0.0\">\n    &lt;\/module>\n&lt;\/config><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 3:<\/strong>&nbsp;Create&nbsp;<strong>app\/code\/Vendor\/Module\/etc\/adminhtml\/routes.xml&nbsp;<\/strong>and add the following code to define the routes:<\/li>\n<\/ul>\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;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:App\/etc\/routes.xsd\">\n    &lt;router id=\"admin\">\n        &lt;route id=\"route\" frontName=\"route\">\n            &lt;module name=\"Vendor_Module\"\/>\n        &lt;\/route>\n    &lt;\/router>\n&lt;\/config><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 4:<\/strong>&nbsp;Add the following code to the&nbsp;<strong>app\/code\/Vendor\/Module\/Controller\/Adminhtml\/Index\/AddRow.php&nbsp;<\/strong>file:<\/li>\n<\/ul>\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\n\nnamespace Vendor\\Module\\Controller\\Adminhtml\\Index;\n\nuse Magento\\Backend\\App\\Action;\nuse Magento\\Framework\\App\\ResponseInterface;\nuse Magento\\Framework\\Controller\\ResultFactory;\nuse Magento\\Framework\\Controller\\ResultInterface;\n\n\/**\n * Class AddRow\n *\/\nclass AddRow extends Action\n{\n    \/**\n     * @return ResponseInterface|ResultInterface\n     *\/\n    public function execute()\n    {\n        $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);\n        $title = __('Your Title');\n        $resultPage->getConfig()->getTitle()->prepend($title);\n        return $resultPage;\n    }\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 5:<\/strong>&nbsp;Create&nbsp;<strong>app\/code\/Vendor\/Module\/view\/adminhtml\/layout\/route_index_addrow.xml&nbsp;<\/strong>with the following code:<\/li>\n<\/ul>\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;?xml version=\"1.0\"?>\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" layout=\"admin-1column\"\n      xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceContainer name=\"content\">\n            &lt;block class=\"Vendor\\Module\\Block\\Adminhtml\\DisplayInfo\" name=\"display_info\" template=\"Vendor_Module::display_info.phtml\" \/>\n        &lt;\/referenceContainer>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 6:<\/strong>&nbsp;Add your desired Phtml file to the&nbsp;<strong>app\/code\/Vendor\/Module\/Block\/Adminhtml\/&nbsp;<\/strong>directory. In this example, the file name will be&nbsp;<em><strong>DisplayInfo.php&nbsp;<\/strong><\/em>with the following sample code:<\/li>\n<\/ul>\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\\Module\\Block\\Adminhtml;\n\/**\n * Class DisplayInfo\n  *\/\nclass DisplayInfo extends \\Magento\\Backend\\Block\\Template\n{\n}<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Step 7:<\/strong>&nbsp;Lastly, use the&nbsp;<strong>app\/code\/Vendor\/Module\/view\/adminhtml\/templates\/display_info.phtml<\/strong>&nbsp;to call the custom Phtml file in Magento 2 admin.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it..!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is how you can use a custom Phtml file in Magento 2 to tweak the layout of the Magento 2 admin panel. I hope this detailed solution will help you insert custom Phtml in Magento 2 admin panel using the module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In case you have any doubts or queries regarding the provided solution, feel free to comment. I\u2019d be happy to help you.  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do not forget to share this Magento solution with your developer friends via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks for reading.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Want to&nbsp;add custom Phtml file in Magento 2 admin? Here\u2019s how you can do that. If you are a Magento developer working on&nbsp;custom development, you&#8230;<\/p>\n","protected":false},"author":38,"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-2184","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2184","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\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=2184"}],"version-history":[{"count":2,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2184\/revisions"}],"predecessor-version":[{"id":9473,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/2184\/revisions\/9473"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=2184"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=2184"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=2184"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}