我正在开发一个购物车插件,并计划为客户创建一个新的用户角色。
我的问题是如何创建自定义功能,以便我可以将这些自定义功能分配给新的用户角色,这个答案提供了一种创建新功能的方法,但它只是原始功能的新名称。
有人能解释一下如何创建一个全新的功能,来控制一些自定义功能吗?
我的问题是如何创建自定义功能,以便我可以将这些自定义功能分配给新的用户角色,这个答案提供了一种创建新功能的方法,但它只是原始功能的新名称。
有人能解释一下如何创建一个全新的功能,来控制一些自定义功能吗?
<?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://wordpress.stackexchange.com/questions/35165/how-do-i-create-a-custom-role-capability
使用现有插件: 用户角色和功能