Plugin Development – Create a Custom WordPress Admin Sub Menu Page
When developing a WordPress plugin, it is often necessary to create custom admin pages to provide additional functionality or settings for the plugin. One common task is to add a sub-menu page under an existing top-level menu in the WordPress admin dashboard. This allows plugin developers to organize their settings or features in a logical manner.
To create a custom WordPress admin sub-menu page, we need to use the add_submenu_page() function. This function allows us to add a new sub-menu page under an existing top-level menu. Here is an example code snippet demonstrating how to create a custom sub-menu page under the “Settings” top-level menu:
function flashify_add_custom_submenu_page() { add_submenu_page( 'options-general.php', // Parent slug 'Custom Submenu Page', // Page title 'Custom Submenu', // Menu title 'manage_options', // Capability 'custom-submenu-page', // Menu slug 'flashify_custom_submenu_page_callback' // Callback function ); } add_action('admin_menu', 'flashify_add_custom_submenu_page'); function flashify_custom_submenu_page_callback() { // Add your custom page content here }
In the above code snippet, we first define a function flashify_add_custom_submenu_page() where we use the add_submenu_page() function to add a new sub-menu page under the “Settings” top-level menu. We specify the parent slug, page title, menu title, capability, menu slug, and callback function.
Next, we create a callback function flashify_custom_submenu_page_callback() where we can add our custom page content. This is where we would typically include HTML forms, settings, or any other content specific to our plugin.
By following this approach, plugin developers can easily create custom admin sub-menu pages in WordPress to enhance the functionality of their plugins. It is important to ensure that the menu slug is unique to avoid conflicts with other plugins or WordPress core functionality.
For more information on creating custom admin pages and sub-menu pages in WordPress plugin development, you can refer to the WordPress Codex documentation.