Only show admin submenus for editors in WordPress. Error results

I use a plugin that adds a submenu item to the admin menu, for example:

add_submenu_page( 'propertyhive', 'Property Hive Settings', 'Settings', 'manage_options', 'ph-settings', 'callback_fn' );

Because of this, by specifying manage_options , it appears only for administrators. I need to show it to editors. Here is what I tried in my topic: 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 one does correctly displays the submenu item, I get the following error:

Sorry, you are not allowed to access this page.

Can anyone see something obvious or have any inclinations as to what might cause this?

Note. . The manage_propertyhive function definitely exists

+6
source share
4 answers

I believe this is because "manage_propertyhive" is not a specific ability, so no one will have access to this menu. You can use one of the predefined Wordpress features that you can find here or you can define your own custom features, such as "manage_propertyhive", following the instructions here .

Hope this helps!

+2
source

1) Are you sure that the add_submenu_page() function from the plugin is copied correctly? add_submenu_page() accepts only 6 parameters - in your question it has 7 parameters with propertyhive , and manage_options menu_slug (which is perplexing)

https://developer.wordpress.org/reference/functions/add_submenu_page/

2) I believe that administrators as well as editors got the ability to manage_propertyhive ? If not convinced.

3) In your sample code, the callback function for the new page is the propertyhive my_theme_callback_fn - did you enter the correct callback function here?

4) The fact that you add a submenu page to the editors does not necessarily mean that they can access this page - have you checked the plugin for further feature checks? Perhaps, in the code of the callback function or in some other function, the capabilities of the plug-in are checked again, and the editors miss some features.

+1
source

That should 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'); 
+1
source

Assuming you have configured this function correctly, the parent page may not allow access to the submenu page, make sure the user has access to the parent page ...

This is a function that checks if the user can access the page ... If it returns false, an error is displayed ...
https://github.com/WordPress/WordPress/blob/4.6.1/wp-admin/includes/plugin.php#L1697-L1763

Besides checking a few other things, it also checks if the parent page is accessible by the user.

If this does not work, I suggest you go to this file for local installation and var_dump all the vars that were marked before returning false , this is how we debug the errors ..;)

Be sure to return the file to the original file (I just update WordPress again, which restores all the main files to their original state) ...

Hope this helps.

0
source

Source: https://habr.com/ru/post/1011485/


All Articles