I was trying to add a submenu item for my Event Calendar Newsletter plugin using the following syntax:
function ecn_pro_license_menu() { add_submenu_page( 'eventcalendarnewsletter', __( 'Event Calendar Newsletter Pro', 'event-calendar-newsletter-pro' ), __( 'Pro Settings', 'event-calendar-newsletter' ), 'add_users', 'ecn-pro', 'ecn_pro_license_page' ); } add_action( 'admin_menu', 'ecn_pro_license_menu' );
Where eventcalendarnewsletter is the slug of the parent menu item, ecn-pro the slug for the submenu item, and ecn_pro_license_page the function to render the settings page.
While the submenu item appeared, it was not working as the url was wrong:
It should be /wp-admin/admin.php?page=ecn-pro, not /wp-admin/ecn-pro. Double checked the syntax, looked at other examples, and all checked out…
The issue was that the add_submenu_page() function was firing before the add_menu_page(), due to the priority of the admin_menu action call. Adding a later priority sorted out the issue (in the add_action call):
function ecn_pro_license_menu() { add_submenu_page( 'eventcalendarnewsletter', __( 'Event Calendar Newsletter Pro', 'event-calendar-newsletter-pro' ), __( 'Pro Settings', 'event-calendar-newsletter' ), 'add_users', 'ecn-pro', 'ecn_pro_license_page' ); } add_action( 'admin_menu', 'ecn_pro_license_menu', 99 );