简体   繁体   English

向WordPress中的编辑器显示仅限管理员的子菜单链接。导致错误

[英]Show admin-only submenu link to editors in WordPress. Results in error

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. 由于它说明了manage_options ,它只对管理员显示。 I need to show it for editors. 我需要为编辑展示它。 Here's what I've tried in my theme's functions.php file: 这是我在我的主题的functions.php文件中尝试过的:

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 :manage_propertyhive能力肯定存在的

I believe this is happening because 'manage_propertyhive' is not a defined capability, therefore nobody will have access to that menu. 我相信这种情况正在发生,因为'manage_propertyhive'不是一个已定义的功能,因此没有人可以访问该菜单。 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 . 您可以使用此处可以找到的预定义wordpress功能之一,也可以按照此处的说明定义自己的自定义功能,例如“manage_propertyhive”。

Hope this helps! 希望这可以帮助!

1) Are you sure the add_submenu_page() function from the Plugin is copied correctly? 1)你确定插件中的add_submenu_page()函数被正确复制了吗? 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) add_submenu_page()只接受6个参数 - 在你的问题中,它有7个参数,其中propertyhive是功能, manage_optionsmenu_slug (这是令人困惑的)

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

2) I guess that administrators as well as editors got the capability manage_propertyhive ? 2)我猜管理员和编辑都有能力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? 3)在您的示例代码中,新propertyhive子菜单页面的回调函数是my_theme_callback_fn - 您是否在此处插入了正确的回调函数?

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? 4)您将子菜单页面添加到编辑器这一事实并不一定意味着他们可以访问该页面 - 您是否检查了插件以进行进一步的功能检查? 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');

Assuming you've set up the capability right, perhaps the parent page is not allowing access to submenu page, make sure user is able to access parent page... 假设您已经设置了正确的功能,也许父页面不允许访问子菜单页面,请确保用户能够访问父页面...

This is the function that checks if user can access the page... If it returns false the error is displayed... 这是检查用户是否可以访问页面的函数...如果返回false,则显示错误...
https://github.com/WordPress/WordPress/blob/4.6.1/wp-admin/includes/plugin.php#L1697-L1763 https://github.com/WordPress/WordPress/blob/4.6.1/wp-admin/includes/plugin.php#L1697-L1763

Besides checking several other things, it also checks if parent page is accessible by the user. 除了检查其他几个外,它还检查用户是否可以访问父页面。

If that doesn't work, I'd suggest you to go to this file on you local install, and var_dump all vars that are checked before returning false , this is how we devs debug the errors... ;) 如果这不起作用,我建议你在本地安装上转到这个文件,并且var_dump在返回false之前检查所有变量,这就是我们开发调试错误的方法......;)

Make sure to restore the file to original file ( I just update the WordPress again, that restores all core files to their original state )... 确保将文件恢复为原始文件(我只是再次更新WordPress,将所有核心文件恢复到原始状态)...

Hope that helps. 希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM