首先查看角色具有哪些权限:

$admin_role_set = get_role( 'administrator' )->capabilities;
$author_role_set = get_role( 'author' )->capabilities;

注意:修改权限的行为是永久性的,除非你手动删除该权限,因为角色的权限设置是保存到数据库中的(保存在数据表 wp_options,字段名称option_name,值为 wp_user_roles),

SQL 命令:

SELECT * FROM `wp_options` WHERE option_name = 'wp_user_roles';

可以通过两种方式添加权限:

<?php
global $wp_roles; // global class wp-includes/capabilities.php
$wp_roles->add_cap( $role, $cap );
?>
or
<?php
$role = get_role( 'author' );
$role->add_cap( $cap );
?>

Parameters 参数介绍:

$cap 代表权限名称,为字符串,如 'unfiltered_html'

Example:

function add_theme_caps() {
// gets the author role
$role = get_role( 'author' ); // This only works, because it accesses the class instance.
// would allow the author to edit others' posts for current theme only
$role->add_cap( 'edit_others_posts' );
}
add_action( 'admin_init', 'add_theme_caps');

为特定用户添加功能权限:

$user = new WP_User( $user_id );
$user->add_cap( 'can_edit_posts' );

add_cap  的定义位于下列文件中:

wp-includes/class-wp-role.php

wp-includes/class-wp-roles.php

wp-includes/class-wp-user.php

unfiltered_html 权限允许用户在文章或评论等地方插入 js 代码等特殊标签,但是只有管理员以及 Editor 默认有该权限,具体权限表请查阅:

https://codex.wordpress.org/Roles_and_Capabilities#Author

如果需要为 Author 添加该权限,需要在 functions.php 中增加如下代码:

$author_role_add_cap = get_role('author');
$author_role_add_cap->add_cap('unfiltered_html');
// Add unfiltered html for editor authority
function km_add_unfiltered_html_capability_to_editors( $caps, $cap, $user_id ) {
if ( 'unfiltered_html' === $cap && ( user_can( $user_id, 'editor' ) || user_can( $user_id, 'author' ) ) ) {
$caps = array( 'unfiltered_html' );
}
return $caps;
}
add_filter( 'map_meta_cap', 'km_add_unfiltered_html_capability_to_editors', , );

增加以上代码后,就可以在文章编辑中插入 js 代码。

资料来源:

https://wordpress.stackexchange.com/questions/227411/how-to-get-all-capabilities-of-an-existing-user-role

https://codex.wordpress.org/Function_Reference/add_cap

Wordpress 为用户或角色 role 添加 capabilities(权限)的更多相关文章

  1. SQL Server 用角色(Role)管理数据库权限

    当数据库越来越多,连接到数据库的应用程序,服务器,账号越来越多的时候,为了既能达到满足账号操作数据权限需求,又不扩大其操作权限,保证数据库的安全性,有时候需要用角色来参与到权限管理中,通过角色做一个权 ...

  2. 如何重置Magento管理用户、角色和资源的权限

    场景1:所有的资源权限被设置为管理角色 步骤1:获取当前的管理角色详细信息 SELECT * FROM admin_role WHERE role_name = 'Administrators' /* ...

  3. 在Postgresql中添加新角色(Role)

    Postgresql安装完成之后,默认会创建名为postgres的用户.角色(Role)和数据库(Database).而使用你自己原有的用户运行psql时会提示错误. bob@localhost:~$ ...

  4. MySQL 8.0用户和角色管理

    MySQL 8.0用户和角色管理 MySQL8.0新加了很多功能,其中在用户管理中增加了角色的管理,默认的密码加密方式也做了调整,由之前的sha1改为了sha2,同时加上5.7的禁用用户和用户过期的设 ...

  5. Oracle新建用户、角色,授权,建表空间

    oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create table权限允许用户创建表,gr ...

  6. Oracle创建用户、角色、授权、建表

    oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create table权限允许用户创建表,gr ...

  7. [转载]Oracle创建用户、角色、授权、建表

    出处:https://www.cnblogs.com/roger112/p/7685307.html oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system p ...

  8. 8.13Oracle新建用户、角色,授权

    oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create table权限允许用户创建表,gr ...

  9. Oracle创建用户、角色、授权、建表空间

    oracle数据库的权限系统分为系统权限与对象权限.系统权限( database system privilege )可以让用户执行特定的命令集.例如,create table权限允许用户创建表,gr ...

随机推荐

  1. JS显示上一周

    <html> <head> <script> var currDT; var aryDay = new Array("日","一&qu ...

  2. ZooKeeper 完全分布式集群环境搭建

    1. 搭建前准备 示例共三台主机,主机IP映射信息如下: 192.168.32.101 s1 192.168.32.102 s2 192.168.32.103 s3 2.下载ZooKeeper, 以  ...

  3. numpy+pandas+matplotlib+tushare股票分析

    一.数据导入 安装tushare模块包 pip install tushare http://tushare.org/ tushare是一个财经数据接口包 import numpy as np imp ...

  4. 多线程, Thread类,Runnable接口

    多线程 线程:线程是进程中的一个执行单元,负责当前进程中程序的执行,一个进程中至少有一个线程.一个进程中是可以有多个线程的,这个应用程序也可以称之为多线程程序. 单线程程序:即,若有多个任务只能依次执 ...

  5. ARC下需要注意的内存问题

    之前发了一篇关于图片加载优化的文章,还是引起很多人关注的,不过也有好多人反馈看不太懂,这次谈谈iOS中ARC的一些使用注意事项,相信做iOS开发的不会对ARC陌生啦.这里不是谈ARC的使用,只是介绍下 ...

  6. turtle画玫瑰花

    import turtle turtle.screensize(400, 300, "pink") turtle.setup(1000, 600) turtle.write('作者 ...

  7. 连接MYSQL 错误代码2003

    问题是服务里面mysql没有启动或者mysql服务丢失 解决办法: 开始->运行->cmd,进到mysql安装的bin目录(以我的为例,我的安装在D盘)D:\MySQL\bin>my ...

  8. [异常笔记] spring boot 启动-2018040201

    异常 1.编码引发异常 00:59:49.311 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - ...

  9. 精读《setState 做了什么》

    1 引言 setState 是 React 框架最常用的命令,它是用来更新状态的,这也是 React 框架划时代的功能. 但是 setState 函数是 react 包导出的,他们又是如何与 reac ...

  10. jquery横向手风琴效果2

    <!doctype html> <html> <head> <meta charset="utf-8"> <script ty ...