WordPress创建自定义功能

时间:2013-01-31 02:57:08

标签: wordpress user-roles capability

我正在开发购物车插件,并计划为客户创建新的用户角色。

我的问题是如何创建自定义功能以便我可以将这些自定义功能分配给新用户角色,this answer提供了一种创建新功能的方法,但它只是原始功能的新名称。

任何人都可以解释如何创建一个控制某些自定义功能的全新功能吗?

2 个答案:

答案 0 :(得分:1)

您应该首先了解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/

答案 1 :(得分:0)

您可以通过插件创建自定义角色和功能。自定义代码提供了两个选项,您也可以使用现有的插件。

对于自定义代码: https://wordpress.stackexchange.com/questions/35165/how-do-i-create-a-custom-role-capability

使用现有插件: User Roles and Capabilities