You shouldn’t use Roles to handle permissions for your menu page. Instead, use capabilities, and then assign those capabilities to whichever roles should have access.
For example, instead of creating a customrole
role, use a custom capability like manage_custom_plugin
:
add_menu_page(
'Custom-Plugin',
'Custom-Plugin',
'manage_custom_plugin',
'custom-plugin',
'init_custom_menu_page'
);
Now, using the Members plugin you can enter manage_custom_plugin
into the “Custom Capability” box, and add it to whichever roles you need.
In code you would grant this capability to roles using WP_Roles::add_cap()
, like so:
$roles = wp_roles();
$roles->add_cap( 'administrator', 'manage_custom_plugin' );
$roles->add_cap( 'editor', 'manage_custom_plugin' );
Just be aware that these functions write to the database, so should only be run once, on plugin or theme activation.