I'm using a plugin which adds a submenu item to an admin menu like so:
add_submenu_page( 'propertyhive', 'Property Hive Settings', 'Settings', 'manage_options', 'ph-settings', 'callback_fn' );
Due to it stating manage_options
it only appears for administrators. I need to show it for editors. Here's what I've tried in my theme's functions.php file:
add_action( 'admin_menu', 'custom_settings_menu', 99 );
function custom_settings_menu()
{
// Remove the submenu item first
remove_submenu_page( 'propertyhive', 'ph-settings' );
// Add it again but with different role (manage_propertyhive)
// This role does exist as other submenu items ue it
add_submenu_page( 'propertyhive', 'Property Hive Settings', 'Settings', 'manage_propertyhive', 'ph-settings', 'my_theme_callback_fn' );
}
Although this does show the submenu item correctly, I get the following error:
Sorry, you are not allowed to access this page.
Can anyone see anything obvious or have any inclinations as to what might cause this?
Note: The manage_propertyhive capability definitely does exist
I believe this is happening because 'manage_propertyhive' is not a defined capability, therefore nobody will have access to that menu. You can either use one of the predefined wordpress capabilities which you can find here or you can define your own custom capability such as 'manage_propertyhive', by following the instructions here.
Hope this helps!
1) Are you sure the add_submenu_page()
function from the Plugin is copied correctly? add_submenu_page()
accepts only 6 parameters - in your question it has 7 paramters with propertyhive
being the capability and manage_options
being the menu_slug
(which is perplexing)
https://developer.wordpress.org/reference/functions/add_submenu_page/
2) I guess that administrators as well as editors got the capability manage_propertyhive
? If not make sure.
3) In your sample code the callback function for the new propertyhive
submenu page is my_theme_callback_fn
- did you insert the correct callback function here?
4) The fact that you add the submenu page to editors does not necessarily mean they can access that page - did you check the Plugin for further capability checks? Maybe in the code of the callback function or in some other function of the Plugin capabilities are checked again and editors are missing some capability.
This must do the trick
function add_theme_caps() {
$role = get_role( 'editor' );
$caps = (array)$role->capabilities;
if(!array_key_exists('manage_propertyhive', $caps)) {
$role->add_cap( 'manage_propertyhive' );
}
}
add_action( 'admin_init', 'add_theme_caps');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With