Backend Menu
The Backend Menu service allows developers to manage access to WordPress admin menu items and submenus programmatically through the AAM Framework. It provides methods to restrict, allow, inspect, and reset backend menu permissions for any access level supported by AAM.
This service is designed to work with WordPress admin menus generated through the global $menu and $submenu structures.
Definition
class AAM_Framework_Service_BackendMenu {
public deny(string $menu_slug) : bool
public allow(string $menu_slug) : bool
public reset(string $menu_slug = null) : bool
public is_denied(string $menu_slug) : bool
public is_allowed(string $menu_slug) : bool
}
Overview
The Backend Menu service provides the ability to:
- Restrict access to admin menu items
- Allow access to previously restricted items
- Reset menu permissions
- Inspect menu visibility state
- Retrieve structured backend menu information
- Work with top-level menus and submenu items
- Normalize WordPress menu slugs consistently
- Integrate custom permission logic via WordPress filters
Accessing the Service:
$menu_service = AAM::api()->backend_menu();
Menu items are identified by normalized menu slugs.
Top-level menus are automatically prefixed with menu/.
Example:
menu/index.php
menu/plugins.php
menu/edit.php
Submenu items are stored without the menu/ prefix.
Example:
plugin-install.php
options-general.php?page=my-plugin
post-new.php?post_type=book
Methods
deny()
Restrict access to a backend menu item.
public function deny(string $menu_slug) : bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$menu_slug | string | Backend menu slug |
Returns
| Type | Description |
|---|---|
bool | true on success |
WP_Error | On failure |
Example
Restrict access to Plugins menu:
AAM::api()
->backend_menu()
->deny('menu/plugins.php');
Restrict access to Settings submenu:
AAM::api()
->backend_menu()
->deny('options-general.php');
Restrict custom post type menu:
AAM::api()
->backend_menu()
->deny('menu/edit.php?post_type=book');
allow()
Explicitly allow access to a backend menu item.
public function allow(string $menu_slug) : bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$menu_slug | string | Backend menu slug |
Returns
| Type | Description |
|---|---|
bool | true on success |
WP_Error | On failure |
Example
AAM::api()
->backend_menu()
->allow('menu/plugins.php');
reset()
Reset permissions for a specific menu item or all backend menu permissions.
public function reset(string $menu_slug = null) : bool
Parameters
| Parameter | Type | Description | |
|---|---|---|---|
$menu_slug | `string | null` | Optional menu slug |
Returns
| Type | Description |
|---|---|
bool | true on success |
WP_Error | On failure |
Example
// Reset Single Menu
AAM::api()
->backend_menu()
->reset('menu/plugins.php');
// Reset All Backend Menu Permissions
AAM::api()
->backend_menu()
->reset();
is_denied()
Check if a menu item is restricted.
public function is_denied(string $menu_slug) : bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$menu_slug | string | Backend menu slug |
Returns
| Type | Description |
|---|---|
bool | true if restricted |
WP_Error | On failure |
Example
if (
AAM::api()
->backend_menu()
->is_denied('menu/plugins.php')
) {
// Access denied
}
is_allowed()
Check if a menu item is allowed.
public function is_allowed(string $menu_slug) : bool
Parameters
| Parameter | Type | Description |
|---|---|---|
$menu_slug | string | Backend menu slug |
Returns
| Type | Description |
|---|---|
bool | true if allowed |
WP_Error | On failure |
Example
if (
AAM::api()
->backend_menu()
->is_allowed('menu/plugins.php')
) {
// Access allowed
}
Additional Public Methods
Although not included in the simplified interface definition, the service also exposes helper methods for retrieving menu structures.
get_items()
Retrieve all backend menu items with permission metadata.
public function get_items() : array
Returns
[
[
'slug' => 'menu/plugins.php',
'path' => '/wp-admin/plugins.php',
'name' => 'Plugins',
'capability' => 'activate_plugins',
'is_restricted' => false,
'children' => [...]
]
]
Example
$items = AAM::api()
->backend_menu()
->get_items();
items()
Alias of get_items().
Example
$items = AAM::api()
->backend_menu()
->items();
get_item()
Retrieve a single menu item.
public function get_item(string $menu_slug) : array
Example
$item = AAM::api()
->backend_menu()
->get_item('menu/plugins.php');
item()
Alias of get_item().
Example
$item = AAM::api()
->backend_menu()
->item('menu/plugins.php');
Menu Normalization
The service normalizes menu slugs internally to ensure consistency. Query parameters are sorted alphabetically.
Example:
edit.php?post_type=book&foo=bar
and
edit.php?foo=bar&post_type=book
become identical normalized slugs.
Parent Menu Resolution
For submenu items, the service automatically determines the parent top-level menu and inherits restrictions from it.
Example:
menu/edit.php
restricts:
post-new.php
post.php
edit.php
unless explicitly allowed.
Special Handling for post.php
The service contains additional logic for WordPress post management screens.
Because WordPress reuses edit.php for all post types, AAM dynamically detects the associated post type to properly resolve permissions.
Example:
post.php?post=123
may resolve internally to:
menu/edit.php?post_type=book
This prevents accidental restriction inheritance across unrelated custom post types.
Dashboard Exception
The default WordPress Dashboard (index.php) is always allowed.
The following menu cannot be denied through is_denied() logic:
menu/index.php
Permission Inheritance
Menu permissions cascade from parent menu items to submenu items.
Example:
deny('menu/tools.php');
implicitly denies access to:
tools.php
site-health.php
export.php
import.php
unless explicitly overridden.
WordPress Filters
aam_backend_menu_is_denied_filter
Customize backend menu permission decisions.
apply_filters(
'aam_backend_menu_is_denied_filter',
$result,
$slug,
$resource,
$parent_slug
);
Parameters
| Parameter | Description |
|---|---|
$result | Current permission state |
$slug | Normalized menu slug |
$resource | Backend menu resource object |
$parent_slug | Parent menu slug |
Example
add_filter(
'aam_backend_menu_is_denied_filter',
function ($result, $slug) {
if ($slug === 'menu/plugins.php') {
return true;
}
return $result;
},
10,
4
);
aam_ignored_backend_menu_item_query_params_filter
Modify ignored query parameters during menu normalization.
Example
add_filter(
'aam_ignored_backend_menu_item_query_params_filter',
function ($params) {
$params[] = 'nonce';
return $params;
}
);
Error Handling
Most service methods return either:
bool
or
WP_Error
Recommended handling:
$result = AAM::api()
->backend_menu()
->deny('menu/plugins.php');
if (is_wp_error($result)) {
echo $result->get_error_message();
}