I'm using add_menu_page(...) from a custom theme and I want to add subpages to that menu with add_submenu_page(...) . If I try to do it from the theme it works as expected but when I try the same from a plugin it just does nothing.
I thought it could be possible that the plugin would be trying to add the submenu page before the menu did even exist, so I wrapped the code in within a function and used add_action(...) from the plugin using a custom action hook created in the place where I wanted the code to execute (in the exact line it worked in the theme) with do_action(...).
The example code would be this:
(theme)
function wvdh_add_admin_page(){
// The next 2 lines work fine
add_menu_page( 'WVDH - Settings','WVDH', 'manage_options', 'wdvh-startpoint', 'render_page', 'dashicons-admin-network', '109');
add_submenu_page( 'wdvh-startpoint', 'page-title', 'menu-title', 'manage_options', 'menu-slug', 'render_page' );
// The next line does nothing
do_action('wvdh-menu');
}
add_action('admin_menu', 'wvdh_add_admin_page');
function render_page(){
echo '<h1>Test</h1>';
}
(plugin)
add_action('admin_menu', 'wvdh_create_menu');
function wvdh_create_menu() {
add_action('wvdh-menu', 'new_subpage');
}
function new_subpage(){
add_submenu_page( 'wvdh-startpoint', 'page-from-plugin', 'menu-from-plugin', 'manage_options', 'plugin-slug', 'wvdh_settings_page' );
}
function wvdh_settings_page() {
echo '<h1>Test from-plugin</h1>';
}