WordPress的capabilities是edit posts, edit other posts, deete posts, 等等, 而permision就是允许edit posts.


点击user-add user可以创建新的用户,用户组角色有订阅者,编辑,管理等五种。但这是通用的角色,怎样创建自定义的角色,来给角色分配自定义的权限,比方说只给某个角色一个post type的编辑权限?

一:插件方法,添加members插件。作者是memberpress. 或者user role editor插件

如何创建自定义的角色?点击members, add new role, 输入名称,可以创建新的角色。然而,我们没看到我们自定义文章类型的内容,这是因为,默认的话,自定义文章类型的permission 和 capbilities 是和post 类型联系在一起,没有单独区别开来。 意思就是当你点击posts, 选择右边的capibilities, 这个role的 capibilities 的应用在了全部posts 和custom post type posts. 因为new custom post types by defalt, 从blog posts继承权限 和capability . 这意思就是当你给则个新定义的role选择post 的capability的时候,这个role也拥有了其他所有自定义文章类型的capability.

所以我们现在要从自定义文章类型的注册代码中添加代码来添加自定义的capability, 比方说你注册的events的自定义的文章类型,我们就是想告诉这个events的文章类型,你的创建权限和capabilities应该和blog posts区分开来,你可以在register_post_type里面加上一条,’capability_type’ => ‘event’, 这样就可以创建自定义的event的capability , 意思就是 当你有权限可以创建post的时候你也有权限可以创建event的帖子。

但是光有这行代码还不够,还需要,in order to actially to force and require this permissions we need to new line ,where we say ‘map_meta_cap’ => true, without this line of code, we would need to program our own custom logic for when these new capabilities should be required. For example if you build some weird crazy website that defined all normal logic, maybe for some weird reason you would want to set things up so that on the fouth day of the month, if your user name starts with the letter s then everyone on the website is allowed to edit events, that’s how flexible how wordpress is. but most of the time we do not need that. so this line of code is our way of just saying , hey wordpress, can you please reasonbaly require event related premissions in order to edit and manage event post types?Can you require the right permission at the right time in a sane fasion for us automatically? So it;s going to automatically map and require the right capability at the right time.

当你创建event role的时候,再去admin的面板看的时候,你就会发现admin的面板已经没有events的菜单了,这是因为我们创建了权限把events的权限单独分配给了event的角色, 所以我们需要在role那里,给管理员添加event的capability的permission.

Share :