The WordPress add_pages_page() is a WordPress builtin function and it will add submenu page to the Pages main menu.
This function takes a capability which will be used to determine whether or not a page is included in the menu. The function which is hooked in to handle the output of the page must check that the user has the required capability as well.
Syntax:
1 2 3 |
add_pages_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', int $position = null ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
public function addMenuItem() { switch ($this->pageType) { case "top": if ($this->actionHookIsValid($this->menuSlug, '')) { add_menu_page($this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array(&$this, 'render')); } break; case "custom": if ($this->actionHookIsValid($this->menuSlug, "edit.php?post_type={$this->targetPostType}")) { add_submenu_page("edit.php?post_type={$this->targetPostType}", $this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array(&$this, 'render')); //add_posts_page($this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array( &$this, 'render' )); } break; case "dashboard": if ($this->actionHookIsValid($this->menuSlug, 'index.php')) { add_dashboard_page($this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array(&$this, 'render')); } break; case "posts": if ($this->actionHookIsValid($this->menuSlug, 'edit.php')) { add_posts_page($this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array(&$this, 'render')); } break; case "pages": if ($this->actionHookIsValid($this->menuSlug, 'edit.php?post_type=page')) { add_pages_page($this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array(&$this, 'render')); } break; case "settings": if ($this->actionHookIsValid($this->menuSlug, 'options-general.php')) { add_options_page($this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array(&$this, 'render')); } break; case "users": if ($this->actionHookIsValid($this->menuSlug, 'user.php') || $this->actionHookIsValid($this->menuSlug, 'profile.php')) { add_users_page($this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array(&$this, 'render')); } break; case "plugins": if ($this->actionHookIsValid($this->menuSlug, 'plugins.php')) { add_plugins_page($this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array(&$this, 'render')); } break; case "theme": if ($this->actionHookIsValid($this->menuSlug, 'themes.php')) { add_theme_page($this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array(&$this, 'render')); } break; default: // Defaults to Tools Menu if ($this->actionHookIsValid($this->menuSlug, 'tools.php')) { add_management_page($this->pageTitle, $this->menuTitle, $this->userCapabilities, $this->menuSlug, array(&$this, 'render')); } break; } return $this; } |
1 2 3 4 5 6 7 8 9 10 11 |
public function __construct($page_title, $page_subtitle, $menu_title, $capability, $menu_slug, $function) { // Call parent constructor to setup everything parent::__construct($page_title, $page_subtitle, $menu_title, $capability, $menu_slug, $function, null, null); // Add pages page add_pages_page($page_title, $menu_title, $capability, $menu_slug, $function); return $this; } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.