2014-02-19 65 views
1

我需要在Wordpress中动态授予用户特定角色(编辑者,管理员等)及其所有功能,但我不想更新他们在数据库中的角色(以便他们下次来回来后,他们会有他们原来的角色)。我怎么能这样做呢?我可以在Wordpress中临时更改用户的角色吗?

+0

http://wordpress.stackexchange.com/questions/53230/temporary-capability-for-current-user-can – berentrom

+0

这是正确的一步方向,但我必须手动覆盖所有的功能吗?我可以轻松地使用此功能返回给定角色的所有功能吗?例如,如果我想将用户的功能设置为编辑器的功能,是否必须手动设置与编辑器相关的所有功能? – Matt

回答

1

这里是我落得这样做:

add_filter('user_has_cap', 'override_caps'); 

function override_caps($allcaps){ 

    if(...){ // When to override caps 

     $role_name = 'administrator'; 
     $role = get_role($role_name); // Get the role object by role name 
     $allcaps = $role->capabilities; // Get the capabilities for the role 
     $allcaps[$role_name] = true;  // Add role name to capabilities 

    } 

    return $allcaps; 

} 
相关问题