This article is associated with the following tutorial series
Building a basic plugin in WordPressOur WordPress plugin might have different menus to handle different things. In this case we’d want to create a sub menu.
First lets create a sub menu class in a file Submenu.php in our inc folder
<?php
namespace Inc;
class Submenu {
}
In our Submenu class, lets make a static function that adds our submenu items called addSubMenuItems
The process to make a sub items in a menu is similar to making menu items, which the difference being we have to define where the parent slug comes from. The parent slug should be the
public static function addSubMenuItems() {
$pages = array(
array(
'parent_slug' => 'victoryflame_plugin',
'page_title' => 'Main Menu',
'menu_title' => 'Main Menu',
'capability' => 'manage_options',
'menu_slug' => 'victoryflame_main',
'callback' => '',
)
);
$page = $pages[0];
add_submenu_page( $page['parent_slug'], $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], $page['callback'] );
}
Now we have a submenu.
We could create a separate view for our new submenu in the View class in View.php
<?php
namespace Inc;
class View {
public function VictoryflameView() {
echo '<h1> I am the page your menu directed you to</h1>';
}
public function SubmenuView() {
echo '<h1>Submenu</h1>';
}
}
and then use it in our callback
<?php
namespace Inc;
use Inc\View;
class Submenu {
public static function addSubMenuItems() {
$view = new View();
$pages = array(
array(
'parent_slug' => 'victoryflame_plugin',
'page_title' => 'Main Menu',
'menu_title' => 'Main Menu',
'capability' => 'manage_options',
'menu_slug' => 'victoryflame_main',
'callback' => array( $view, 'SubmenuView'),
)
);
$page = $pages[0];
add_submenu_page( $page['parent_slug'], $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], $page['callback'] );
}
}