NB: Make sure you do a back up of your theme, files and database before attempting the tutorials
Last modified : Oct 01 2022
Estimated reading time : 1 minute, 43 seconds - 146 words
Share the post "WordPress: Hiding a Sub-Menu with a Plugin"
With this tutorial, we will show you how to hide a sub-menu with a plugin we’re developing all the while keeping the slugs intact if required for developed plugin. (add_submenu_page)
First, we need a menu and a sub-menu for our WordPress plugin.
Let’s take a look at the function below; we see that the plugin has a Main Tab and Sub-Menu tab that are displayed on the WordPress dashboard with the name of the plugin.
///////////////////////////////////////////// Menu Plugin Wordpress //////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_action('admin_menu', 'fonction_menu_page'); function fonction_menu_page() { add_menu_page( 'Titre Du Plugin', 'Titre Du Plugin', 'manage_options', 'slug-du-plugin', 'nomdelafonction', '',2); add_submenu_page('slug-du-plugin', 'Titre Du Sous Menu Plugin', 'Titre Du Sous Menu Plugin', 'manage_options', 'slug-du-sous-menu', 'autrenomdelafonction'); }
We want to keep our Main page of the WordPress plugin, but if we want we can hide the sub-menu.
In order to do so, simply add null in add_submenu_page. This allows us to keep the sub-menu slug in a tab or navigation item of our plugin.
WordPress : Cacher un Sous-Menu Plugin
Here’s the same function with the hidden sub-menu thanks to the null.
///////////////////////////////////////////// Menu Plugin Wordpress //////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// add_action('admin_menu', 'fonction_menu_page'); function fonction_menu_page() { add_menu_page( 'Titre Du Plugin', 'Titre Du Plugin', 'manage_options', 'slug-du-plugin', 'nomdelafonction', '',2); add_submenu_page( null, 'slug-du-plugin', 'Titre Du Sous Menu Plugin', 'Titre Du Sous Menu Plugin', 'manage_options', 'slug-du-sous-menu', 'autrenomdelafonction'); }