WordPress创建自定义权限能力

9
我正在开发一个购物车插件,并计划为客户创建一个新的用户角色。
我的问题是如何创建自定义功能,以便我可以将这些自定义功能分配给新的用户角色,这个答案提供了一种创建新功能的方法,但它只是原始功能的新名称。
有人能解释一下如何创建一个全新的功能,来控制一些自定义功能吗?

1
@maiorano84 谢谢,我已经搜索了一段时间,没有找到完全符合我的要求的东西。 - Minqi
http://wordpress.stackexchange.com/questions/35165/how-do-i-created-a-custom-capability - RRikesh
7
“让我帮你谷歌一下”这句话的讽刺之处在于,正是通过谷歌搜索我才来到了这里。 - IAmJulianAcosta
嗨。你解决了这个问题吗?我也遇到了同样的困境。我想在WooCommerce中仅具备编辑订单状态的能力。 - bronlund
2个回答

8
你首先需要明白的是,WordPress用户角色就是一组能力的简单集合。既然你说你正在创建一个插件,我想你应该不会对代码感到陌生,因此不必害怕编写自己的解决方案,而不是使用另一个插件。
以下代码应该能帮助你创建一个新的用户角色并添加自定义能力。
<?php

// create a new user role

function wpeagles_example_role()
{
    add_role(
        'example_role',
        'example Role',
        [
            // list of capabilities for this role
            'read'         => true,
            'edit_posts'   => true,
            'upload_files' => true,
        ]
    );
}

// add the example_role
add_action('init', 'wpeagles_example_role');

要将自定义功能添加到此用户角色,请使用以下代码:

//adding custom capability
<?php
function wpeagles_example_role_caps()
{
    // gets the example_role role object
    $role = get_role('example_role');

    // add a custom capability 
    // you can remove the 'edit_others-post' and add something else (your     own custom capability) which you can use in your code login along with the current_user_can( $capability ) hook.
    $role->add_cap('edit_others_posts', true);
}

// add example_role capabilities, priority must be after the initial role     definition
add_action('init', 'wpeagles_example_role_caps', 11);

进一步了解: https://developer.wordpress.org/plugins/users/roles-and-capabilities/

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接