Menus
You can set your custom menu in /config/menus.php
file. This file returns an array representing the menu structure. You can leave this array empty if you don’t need a menu. Let me show you a typical menu structure.
if (!defined('ABSPATH')) {
exit();
}
/*
|--------------------------------------------------------------------------
| Plugin Menus routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the menu routes for a plugin.
| In this context, the route is the menu link.
|
*/
return [
'wp_kirk_slug_menu' => [
"page_title" => "WP Kirk Page title",
"menu_title" => "WP Kirk Menu title",
'capability' => 'read',
'position' => null,
'icon' => 'dashicons-admin-plugins',
'items' => [
[
"page_title" => "Main View",
"menu_title" => "Main View",
'capability' => 'read',
'route' => [ 'get' => 'Dashboard\DashboardController@firstMenu' ],
],
[
"page_title" => "Ajax Example",
"menu_title" => "Ajax Example",
'capability' => 'read',
'route' => [ 'get' => 'Dashboard\DashboardController@secondMenu' ],
]
]
]
];
Under the hood
The menu page slug for the first item will be wp_kirk_slug_menu
. For the second item, it will be wp_kirk_1
. For the third item, it will be wp_kirk_2
.
In other words, the menu slug is {namespace}_{index_sub_menu}
.
Of course, you can replace the submenu index with your own array key:
return [
'wp_kirk_slug_menu' => [
"page_title" => "WP Kirk Page title",
"menu_title" => "WP Kirk Menu title",
'capability' => 'read',
'position' => null,
'icon' => 'dashicons-admin-plugins',
'items' => [
'my-main-view' => [
"page_title" => "Main View",
"menu_title" => "Main View",
'capability' => 'read',
'route' => [ 'get' => 'Dashboard\DashboardController@firstMenu' ],
],
'example' => [
"page_title" => "Ajax Example",
"menu_title" => "Ajax Example",
'capability' => 'read',
'route' => [ 'get' => 'Dashboard\DashboardController@secondMenu' ],
]
]
]
];
In this case, you’ll get the first item:
/wp-admin/admin.php?page=wp_kirk_my_main_view
and
/wp-admin/admin.php?page=wp_kirk_example
Under the hood
The array key will be sanitized in lowercase, and any of the character -
will be replaced by _
.
Get a menu item URL
You can get a menu item URL by using the getMenuUrl()
method from your plugin instance. This method accepts the menu slug or the menu index. For example:
$plugin->getMenuUrl('example'); // will return /wp-admin/admin.php?page=wp_kirk_example
or
$plugin->getMenuUrl(1); // will return /wp-admin/admin.php?page=wp_kirk_1
Default values
You may avoid inserting the following array items:
icon : blank
page_title : menu_title
capability : read
position : null
The capability
key is optional for both menu and submenu items. The default value of the key capability
in the root menu is read
when it is not set. The default value of the key capability
in the submenu remains the same as the root menu when it is not set. For example:
if (!defined('ABSPATH')) {
exit();
}
/*
|--------------------------------------------------------------------------
| Plugin Menus routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the menu routes for a plugin.
| In this context, the route is the menu link.
|
*/
return [
'wp_kirk_slug_menu' => [
"page_title" => "WP Kirk Page title",
"menu_title" => "WP Kirk Menu title",
'capability' => 'manage_options',
'position' => null,
'icon' => 'dashicons-admin-plugins',
'items' => [
[
"page_title" => "Main View",
"menu_title" => "Main View",
'route' => [ 'get' => 'Dashboard\DashboardController@firstMenu' ],
]
]
]
];
The submenu “Main View” capability will be set to manage_options
.
Icon
If you set the icon key to something like dashicons-admin-plugins
, the official icon font of the WordPress admin will be used. Otherwise, if the value doesn’t start with dashicons-
, it will be used as an image filename in the public/images/
folder.
'icon' => 'wpbones-logo-menu.png', // /public/images/wpbones-logo-menu.png
Custom Post Type attachment
You could have your own Custom Post Type as a standard WordPress menu. To attach your custom menu below the Custom Post item menu, just replace the first menu key with something like the following one:
return [
'edit.php?post_type=your_cpt_id',
...
Where your_cpt_id
, enter the Custom Post Type ID. So, your menu will be attached under the Custom Post Type submenu items.
Menu View Controller
As you can see from the code above, the view is handled by a Laravel-style syntax. In other words, the view
key is bound to a View Controller’s methods. The syntax is very easy: ViewController@method
.
Of course, you will add the namespace as necessary.