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

\Magento\Framework\Component\ComponentRegistrar::register(

\Magento\Framework\Component\ComponentRegistrar::MODULE,

'Demo_Mymenu',

__DIR__

);

Now create module.xml in app/code/Demo/Mymenu/etc to configure the module and paste the following code into the newly created file:

<?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>

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:

<?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>

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:

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

Log in to the admin panel of your Magento 2 and you will see the new menu if everything was done correctly:

a custom admin menu added

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!