Add a Top-Level Menu
add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', string $icon_url = '', int $position = null );
Example
function wporg_options_page_html() { ?> <div class="wrap"> <h1><? php echo esc_html( get_admin_page_title() ); ?></ h1> <form action="options.php" method="post"> <? php // output security fields for the registered setting "wporg_options" settings_fields( 'wporg_options' ); // output setting sections and their fields // (sections are registered for "wporg", each field is registered to a specific section) do_settings_sections( 'wporg' ); // output save settings button submit_button( __( 'Save Settings', 'textdomain' ) ); ?> </form> </div> <? php }
add_action( 'admin_menu', 'wporg_options_page' ); function wporg_options_page() { add_menu_page( 'WPOrg', 'WPOrg Options', 'manage_options', 'wporg', 'wporg_options_page_html', plugin_dir_url(__FILE__) . ' images/icon_wporg.png', twenty ); }
Using a PHP File for HTML
add_action( 'admin_menu', 'wporg_options_page' ); function wporg_options_page() { add_menu_page( 'WPOrg', 'WPOrg Options', 'manage_options', plugin_dir_path(__FILE__) . ' admin/view.php', null, plugin_dir_url(__FILE__) . ' images/icon_wporg.png', twenty ); }
Remove a Top-Level Menu
remove_menu_page( string $menu_slug );
Example
add_action( 'admin_menu', 'wporg_remove_options_page', 99 ); function wporg_remove_options_page() { remove_menu_page( 'tools.php' ); }
Submitting forms
-
Use the URL of the page as the action attribute of the form. -
Add a hook with the slug, returned by add_menu_page .
Form action attribute
<form action="<?php menu_page_url( 'wporg' ) ?> " method="post">
Processing the form
"load-$hookname"
add_action( 'admin_menu', 'wporg_options_page' ); function wporg_options_page() { $hookname = add_menu_page( 'WPOrg', 'WPOrg Options', 'manage_options', 'wporg', 'wporg_options_page_html', plugin_dir_url(__FILE__) . ' images/icon_wporg.png', twenty ); add_action( 'load-' . $ hookname, 'wporg_options_page_submit' ); }
-
Whether the form is being submitted ( 'POST' === $_SERVER['REQUEST_METHOD'] ). -
CSRF verification -
Validation -
Sanitization