添加新客户用户角色的现有菜单页面

时间:2017-12-15 作者:Neeraj Verma

我已创建新的用户角色“主机”。现在,我试图显示管理菜单项\'用户\'。我在google上搜索了一些代码并尝试了这个

$result = add_role(
    \'Host\',
    __(Host),
    array(
        \'read\'         => true // Use false to explicitly deny
    )
);


function Host_menu_links(){
    $user = wp_get_current_user();
    if( $user_role == \'Host\' ) {
        remove_menu_page(\'tools.php\');
        remove_menu_page(\'themes.php\');
        remove_menu_page(\'options-general.php\');
        remove_menu_page(\'plugins.php\');
    remove_menu_page(\'edit-comments.php\');
    remove_menu_page(\'page.php\');
    remove_menu_page(\'upload.php\');
    remove_menu_page( \'edit.php?post_type=page\' ); 
    remove_menu_page( \'edit.php?post_type=videos\' );
    remove_menu_page( \'edit.php\' );

    }
}


add_action(\'admin_menu\', \'Host_menu_links\');
我试着只给它读权限。但在以用户身份登录后,它不会分配任何菜单页。我不想使用插件。如何修改代码或查找任何其他要修改的函数?

编辑:我还尝试添加add\\u menu\\u page()

    function add_admin_menu_links(){
        $user = wp_get_current_user();

        $user_roles = $user->roles;
    $user_role = array_shift($user_roles);

        if( $user_role == \'subscriber\' ) {

            /* add_menu_page(\'Tax Info\', \'Tax Info\', \'vendor\', \'tax-info\', \'vendor_tax_info_page\');  --option 1 
            add_menu_page(\'users\', \'users\', \'subscriber\', \'users\', \'users.php\');
--option 2 
            add_menu_page( __( \'Custom Menu Title\', \'textdomain\' ),\'custom menu\',\'manage_options\',\'users.php\',\'\');*/
--option 3 

        }
    }
我尝试了3种选择。我试图获取subcriber角色的用户菜单,但单击用户页面时出现错误

\'您没有访问该页面的权限\'

如何修改它?

1 个回复
SO网友:Elex

好的,我给你一个基于能力而非角色的答案。如果您愿意,您将找到一种方法为其他角色隐藏它(您的问题中有代码)。add_menu_page 需要一些参数、挂钩和回调才能工作:https://developer.wordpress.org/reference/functions/add_menu_page/

// Here is your hook to add page to the menu
add_action(\'admin_menu\', \'wpse_288671_add_menu_pages\');

// This function will add a page to your admin and your admin menu
function wpse_288671_add_menu_pages() {
     add_menu_page(
        __( \'User page title\', \'textdomain\' ),
        __( \'User\',\'textdomain\' ), // It\'s your menu title
        \'read\', // This is the capability required to see this, here I use read
        \'user_admin_page\', // Here is your page slug 
        \'wpse_288671_user_admin_page_callback\', // Here is your function name to show content
        \'\', // Here your can add your custom icon URL
        null, // Here your can add the position (int)
    );
}

// This function is the callback used in wpse_288671_add_menu_pages, that\'s the content of your page
function wpse_288671_user_admin_page_callback() {
?>
<h2>Hey bro ! Can your read this ?</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<?php
}
此代码将添加一个菜单链接“User”,单击此链接可显示“User page title”页面,供有能力的人员使用read“(超级管理员、管理员、编辑、作者、贡献者、订阅者和您的主机)。有关文档中角色和功能的更多信息:https://codex.wordpress.org/Roles_and_Capabilities

结束