简体   繁体   English

如何在 wordpress 中为自定义插件定义自定义功能

[英]How to define custom capabilities in wordpress for custom plugin

I create own plugins with their own tables and actions.我使用自己的表和操作创建自己的插件。 In these plugins I didn't make use of custom plugins.在这些插件中,我没有使用自定义插件。

However I want to define custom capabilities and roles.但是我想定义自定义功能和角色。 I want to base them on the action(url parameter).我想将它们基于操作(url 参数)。

Is this possible?这可能吗?

Take a look at the Roles & Capabilities in WordPress for a bit more information on what already exists and what they are used for. 查看WordPress中的“ 角色和功能” ,以获取有关已存在的内容及其用途的更多信息。

Capabilities in WordPress are very basic because when you add a capability to a user, it doesn't actually do anything. WordPress中的功能是非常基本的,因为当您向用户添加功能时,它实际上没有任何作用。 It's when you add features that rely on the capability that it starts to have authority on the site. 就是在您添加依赖于该功能的功能时,它才开始在站点上具有权限。

The add_cap documentation will give you some extra info, however this is how you can add a custom capability to the author role as an example: add_cap文档会给你一些额外的信息,但是这是你如何可以添加自定义功能的author作为例子的作用:

$role = get_role( 'author' );
$role->add_cap( 'my_custom_cap' ); 

That's all there is to it. 这里的所有都是它的。 To remove a capability, use remove_cap instead of the add_cap above and it'll remove it properly from the role for you. 要删除功能,请使用remove_cap而不是上面的add_cap ,它将为您正确地从角色中删除它。

Based on the other answers, I think what you wanna do is to "hook" your custom capability with some actions performed by the user.根据其他答案,我认为您想做的是通过用户执行的某些操作“挂钩”您的自定义功能。 Example: you create and assign a new capability: "create_events", so your user will be able to create events.示例:您创建并分配了一个新功能:“create_events”,因此您的用户将能够创建事件。

You can create all logic in order to create Events and write a simple check on the user's capability to allow this actions or not.您可以创建所有逻辑以创建事件,并对用户是否允许此操作的能力进行简单检查。 This is useful if you want to show some data or not.如果您想显示或不显示某些数据,这很有用。

Example:例子:

 if (current_user_can("create_events")){ //Do something }

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

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