Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Automatically Add WordPress Submenu when use add_menu_page

Hello Friends,

If you were working with WordPress customizations, than I am sure you have used the add_menu_page function of WordPress. It adds a New Menu Item in the admin navigation. I used it for many times, but I saw that when I use it with one sub menu, it automatically adds main menu as first submenu. I was working with Reports section in one of project where I need to generate 3 reports. I adds main menu as Reports and I need to add 3 submenu under it. But When I implement it using add_menu_page and add_submenu_page, it shows 4 submenu under reports. Here is the code.

add_menu_page( "Reports", "Reports", "administrator", "custom_report", "custom_first_report_func","",14);
add_submenu_page( "custom_report", "First Report", "First Report", "administrator", "custom_first_report", "custom_first_report_func" );
add_submenu_page( "custom_report", "Second Report", "Second Report", "administrator", "custom_second_report", "custom_second_report_func" );
add_submenu_page( "custom_report", "Third Report", "Third Report", "administrator", "custom_third_report", "custom_third_report_func" );

I was hoping above code will give me result as below.
Reports
– First Report
– Second Report
– Third Report

But It shows me below result.

Reports
– Reports
– First Report
– Second Report
– Third Report

It adds Reports as first submenu automatically. To remove/prevent the first submenu, there are 2 ways to do it.

1. Give non-existing role to add_menu_page instead of administrator. So it will automatically hide as there is no such role.

add_menu_page( "Reports", "Reports", "noadministrator", "custom_report", "custom_first_report_func","",14);

OR

2. Pass null as function argument in add_menu_page. Look at below code.

add_menu_page( "Reports", "Reports", "administrator", "custom_report", null,"",14);

I hope this helps you to prevent submenu which automatically adds when use add_menu_page. Hide first submenu which under main menu in WordPress. Automatically Add WordPress Submenu when use add_menu_page is solved. 🙂

Share:

No Comments Yet.

Leave a Reply

Your email address will not be published.