微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Wordpress 上用户角色的自定义功能

如何解决Wordpress 上用户角色的自定义功能

这可能有点令人困惑,但我会尽力解释。

我正在尝试创建一种功能,允许用户用户添加到我们的网站。他们也可以查看和删除这些用户,但是为了数据安全,我不希望他们能够查看站点上的所有其他用户,而只能查看他们创建的用户

这是一家在线学习公司,我们正在尝试为“导师”提供添加删除他们自己的“学生”的访问权限,同时看不到网站上的所有其他用户。我们还有其他角色需要不受影响,例如订阅者和管理员

我创建了两个新角色。导师和学生。我遇到的问题是“导师”创建的用户没有显示用户列表中。它完全是空的,根本没有显示任何用户

// 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,'edit_posts' => false,'edit_pages' => false,'edit_others_posts' => false,'create_posts' => false,'manage_categories' => false,'publish_posts' => 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,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' );

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。