如何为用户角色添加功能?
-
16-10-2019 - |
题
我正在管理一个WordPress网络,并希望将未过滤的_HTML用户功能添加到Admin已预定义的用户角色中。在WordPress的标准安装中,Admin帐户已经具有此功能,但是在MU安装中,只有超级管理员可以提供此功能。 WordPress 角色和能力.
如何在主题或插件中增强管理员角色?
解决方案
您可以使用 wp_role 班级,
// get the the role object
$role_object = get_role( $role_name );
// add $cap capability to this role object
$role_object->add_cap( $capability_name );
// remove $cap capability from this role object
$role_object->remove_cap( $capability_name );
因此,要解决您的原始问题,即如何使管理员输入脚本和iframe标签到帖子内容中,您正在寻找“ unftered_html”功能,在多站点中仅授予超级管理员。
// get the the role object
$admin_role = get_role( 'administrator' );
// grant the unfiltered_html capability
$admin_role->add_cap( 'unfiltered_html', true );
或者,您可以在您的功能中运行一次:
/* Roles & Capabilities */
add_role('professional', 'Professional User', array(
'read' => true, // True allows that capability, False specifically removes it.
'edit_posts' => true,
'delete_posts' => true,
//'edit_published_posts' => true,
//'publish_posts' => true,
//'edit_files' => true,
'upload_files' => true //last in array needs no comma!
));
其他提示
为了允许超级管理员或管理员以外的其他角色(取决于WordPress安装是否为网络/MU实例),必须删除未过滤的HTML或评论WordPress'KSES滤波器。
检查用户是否具有特定功能。
if ( current_user_can( 'unfiltered_html' ) ) { … }
如果是这样,则删除KSE
kses_remove_filters();
该功能已经包裹在未过滤的MU中,使管理员和编辑可以添加未经过滤的HTML。