Magento 2 is a robust platform with loads of useful options and features. But from a development point of view, sometimes the provided options don’t fulfill all the requirements of a project, hence, custom options are required to complete some tasks. And a custom admin menu is among those you can implement yourself to improve your Magento admin workflow.
Today, in this guide, you are going to learn how to add a custom admin menu in Magento 2.
First, I will create a custom module and then a custom admin menu!
Configure and register a custom module
Create registration.php in app/code/Demo/Mymenu/ to register the module. Paste this code to it:
[php]<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
‘Demo_Mymenu’,
__DIR__
);[/php]
Now create module.xml in app/code/Demo/Mymenu/etc to configure the module and paste the following code into the newly created file:
[php]<?xml version=”1.0″?>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:noNamespaceSchemaLocation=”urn:magento:framework:Module/etc/module.xsd”>
<module name=”Demo_Mymenu” setup_version=”1.0.0″ />
</config>[/php]
Create a custom admin menu
Create menu.xml in app/code/Demo/Mymenu/etc/adminhtml to create a custom admin menu and paste the following code into the newly created file:
[php]<?xml version=”1.0″?>
<config xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:noNamespaceSchemaLocation=”urn:magento:module:Magento_Backend:etc/menu.xsd”>
<menu>
<add id=”Demo_Mymenu::first_level_demo”
title=”My Menu”
module=”Demo_Mymenu”
sortOrder=”20″
resource=”Magento_Backend::content” />
<add id=”Demo_Mymenu::second_level_demo”
title=”Sub Menu”
module=”Demo_Mymenu”
sortOrder=”1″
action=”menuitem/index/index”
parent=”Demo_Mymenu::first_level_demo”
resource=”Magento_Backend::content” />
</menu>
</config>[/php]
In the code above, there are two <add> attributes. The first one is for first level menu, and the other one is for second level attribute.
Here’s the explanation of the entities used in the code:
id: is used as the unique identifier of a menu.
title: the menu title that will be displayed in the admin menu.
module: the module that I have created (Demo_Mymenu).
sortOrder: is used to set the placement of menu.
resource: a rule to identify which admin user can see this menu.
action: is used to link an admin controller
parent: is used to define which menu level it depends on.
Launch the SSH terminal
All done! Now just connect your store using SSH and run these commands in the root directory of the store:
[php]rm -rf var/di var/generation var/cache/* var/log/* var/page_cache/*
php bin/magento module:enable Demo_Mymenu
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento indexer:reindex
php bin/magento cache:clean
php bin/magento cache:flush[/php]
Log in to the admin panel of your Magento 2 and you will see the new menu if everything was done correctly:
I hope after following this tutorial you can now successfully add a custom admin menu in Magento 2. If you still have any confusions, just leave a comment below and I will get back to you!
Hey Syed,
This looks great. Sorry I’m learning, so if my question does not illustrate properly please bear with me. What I’m trying to do is create a register/ login screen from my main (default) page, but i wanted it to have 2 different kinds of login/register. 1)personal, 2) Business… I have already created a personal store, and a business store and they would both have different information on each (B2C, AND B2B). What my goal is to get rid of the login, register from all 3 stores (main (default), personal, and Business) and just put it on the middle of the screen of main instead of it being on the top right hand corner (that’s all i want on the main screen), but once logged in it would take you to the appropriate store.
would it be possible for you to advise me on how to do this?
Thank you Syed,