In this blog, we will learn how to create custom WordPress Administrative Pages and cover following points.

  • Adds a new submenu item to the existing WordPress menu system.
  • Adds a new settings page that corresponds to the new submenu item.
  • Sanitizes and serializes options on the page.
  • Validates and returns the values that were saved and renders them accordingly.
add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', string $icon_url = '', int $position = null );

This code adds a top-level menu page.

Parameters :

$page_title (string)(Required) : The text to be displayed in the title tags of the page when the menu is selected.

$menu_title (string)(Required) : The text to be used for the menu.

$capability (string)(Required) : The capability required for this menu to be displayed to the user.

$menu_slug (string)(Required) : The slug name to refer to this menu by (should be unique for this menu).

$function (callable)(Optional) :  The function to be called to output the content for this page.Default value: ”

$icon_url (string)(Optional) :  The URL to the icon to be used for this menu.

$position (int)(Optional) :  The position in the menu order this one should appear. Default value: null

To do this, we’ll need to take advantage of the WordPress API function add_options_page. This function will require five parameters:

  1. the text to display as the title of the corresponding options page
  2. the text to display as the submenu text for the menu item
  3. the capabilities needed to access this menu item
  4. the menu slug that’s used to identify this submenu item
  5. a callback to a function that’s responsible for rendering the admin page
add_submenu_page( string $parent_slug, string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '' );

Above code adds a submenu page.

Parameters :

$parent_slug (string)(Required) : The slug name for the parent menu (or the file name of a standard WordPress admin page).

$page_title (string)(Required) : The text to be displayed in the title tags of the page when the menu is selected.

$menu_title (string)(Required) : The text to be used for the menu.

$capability (string)(Required) : The capability required for this menu to be displayed to the user.

$menu_slug (string)(Required) : The slug name to refer to this menu by (should be unique for this menu).

$function (callable)(Optional) : The function to be called to output the content for this page.
Default value: ”

Example:  Copy paste the code in functions.php file of your child theme.

add_action( 'admin_menu',  'add_options_page'  );
/**
* Creates the submenu item and calls on the Submenu Page object to render
* the actual contents of the page.
*/
function add_options_page() {
  add_options_page(
    'WB Custom Administration Page',
    'Custom Administration Page',
    'manage_options',
    'custom-admin-page',
    'render'
  );
}

Add the following code in functions.php file of your child theme.

// Creates a submenu page
function render() {
  echo 'This is the basic submenu page.';
}
Welcome to Wbcom Designs!  - Get 10% off on all plugins & themes
Coupon : HELLO10