这可能有点令人困惑,但我会尽力解释。
我正在尝试创建一种功能,允许用户将用户添加到我们的网站。他们也可以查看和删除这些用户,但是为了数据安全,我不希望他们只能查看他们创建的用户在站点上的所有其他用户。
这是一家在线学习公司,我们正在尝试为“导师”提供添加和删除他们自己的“学生”的访问权限,同时看不到网站上的所有其他用户。我们还有其他角色需要保持不受影响,例如订阅者和管理员。
我创建了两个新角色。导师和学生。我遇到的问题是“导师”创建的用户未显示在用户列表中。它完全是空的,根本没有显示任何用户。
// Add a custom user role
$result = add_role( 'tutors', __(
'Tutors' ),
array(
'edit_users' => true,
'create_users' => true,
'delete_users' => true,
'list_users' => true,
'read' => true,
'edit_posts' => true,
'edit_pages' => true,
'edit_others_posts' => true,
'create_posts' => true,
'manage_categories' => true,
'publish_posts' => true,
'edit_themes' => false,
'install_plugins' => false,
'update_plugin' => false,
'update_core' => false,
)
);
$result1 = add_role( 'student', __('Student'),
array(
'edit_users' => false,
'create_users' => false,
'delete_users' => false,
'read' => true,
'edit_posts' => false,
'edit_pages' => false,
'edit_others_posts' => false,
'create_posts' => false,
'manage_categories' => false,
'publish_posts' => false,
'edit_themes' => false,
'install_plugins' => false,
'update_plugin' => false,
'update_core' => false
)
);
/**
* Admin New Tutor Function
* @var int $user_id
* @return void
*/
function student_register( $user_id ) {
if( ! is_admin() ) {
return;
}
// Grab the current user
$current_user = wp_get_current_user();
// IF the current user ID isn't 0 and our current user is a 'tutors' role
if( $current_user->ID && in_array( 'tutors', $current_user->roles ) ) {
// Update the new user with a 'parent' usermeta value of the current 'tutors'
update_user_meta( $user_id, '_user_parent', $current_user->ID );
}
}
add_action( 'user_register', 'student_register' );
/**
* Pre Get Users filter
* @var WP_Query Object $query
* @return void
*/
function theme_pgu( $query ) {
if( ! is_admin() ) {
return;
}
// Grab our current user
$current_user = wp_get_current_user();
// IF our user ID is not 0 and our current user has a role of 'tutors'
if( $current_user->ID && in_array( 'tutors', $current_user->roles ) ) {
// Set the query to only return student roles
$query->set( 'role', 'student' );
// Which has a usermeta key '_user_parent' set
$query->set( 'meta_key', '_user_parent' );
// and has a usermeta value of the current tutor user
$query->set( 'meta_value', $current_user->ID );
}
}
add_action( 'pre_get_users', 'theme_pgu' );
/**
* Selectable roles on the new user and user edit screen
* @var Multi-dimensional Array $roles
* @return Array $roles
*/
function client_sel_roles( $roles ) {
// Grab our current user
$current_user = wp_get_current_user();
if( in_array( 'tutors', $current_user->roles ) ) {
$roles = array( 'student' => $roles['student'] );
}
return $roles;
}
add_filter( 'editable_roles', 'client_sel_roles' );
/**
* All Users screen filterable views
* @var Array $views
* @return Array $views
*/
function client_user_views( $views ) {
// Grab our current user
$current_user = wp_get_current_user();
if( in_array( 'tutors', $current_user->roles ) ) {
if( isset( $views['student'] ) ) {
$views = array( 'student' => $views['student'] );
} else {
$views = array();
}
}
return $views;
}
add_filter( 'views_users', 'client_user_views' );
/**
* Stop clients from changing the URL to get to other profiles
* @var WP_Screen Object $screen
* @return void
*/
function edit_students_only( $screen ) {
// Check if we're on the correct screen
if( 'user-edit' === $screen->base ) {
// Ensure our desired user ID is set
if( isset( $_GET['user_id'] ) && is_numeric( $_GET['user_id'] ) ) {
$user_id = absint( $_GET['user_id'] );
$current_user = wp_get_current_user();
$parent = get_user_meta( $user_id, '_user_parent', true );
// Ensure that we're viewing a profile that is not our own
if( $current_user->ID && in_array( 'tutors', $current_user->roles ) && $user_id !== $current_user->ID && $parent !== $current_user->ID ) {
// We're viewing an incorrect profile - redirect to clients own profile
wp_redirect( admin_url( "user-edit.php?user_id={$current_user->ID}" ) );
}
}
}
}
add_action( 'current_screen', 'edit_students_only' );