WordPress 为指定角色添加删除权限

江河/ 2023年08月12日/ WordPress/ 浏览 597

在 WordPress 中修改指定角色的权限,需要借助 WP_Role 操作。


下面的代码,为 simple_role 添加 edit_others_posts 权限。


function wporg_simple_role_caps() {
	// Gets the simple_role role object.
	$role = get_role( 'simple_role' );

	// Add a new capability.
	$role->add_cap( 'edit_others_posts', true );
}

// Add simple_role capabilities, priority must be after the initial role definition.
add_action( 'init', 'wporg_simple_role_caps', 11 );


WP_Role 的几个重要方法:


add_cap -- 为角色分配功能。


has_cap -- 确定角色是否具有给定的功能。


remove_cap -- 从角色中删除功能。


发表评论

暂无评论,抢个沙发...

客服 工单