• Resolved jonny-s

    (@jonny-s)


    Hi,

    Is there a way to exclude users by role from the users-listing page?
    (I know about the option to hide individual users, but I want to hide all subscribers and admin.)

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi,

    Try following code in functions.php file of currently active theme or via Code Snippet plugin and let me know if it works or not:

    add_filter('uwp_excluded_users_from_list', 'uwp_excluded_users_from_list_cb', 10, 1);
    function uwp_excluded_users_from_list_cb($exclude_users){
    	$users = get_users( array( 'role__in' => array( 'subscriber', 'administrator' ), 'fields' => array('ID') ) );
    	$users = wp_list_pluck( $users, 'ID' );
    	if($users && count($users) > 0){
            return array_merge($exclude_users, $users);
        }
    	return $exclude_users;
    }

    Regards,
    Patrik

    Thread Starter jonny-s

    (@jonny-s)

    Hi Patrik,

    thanks for the code, I’ll try it later on!

    Thread Starter jonny-s

    (@jonny-s)

    Hi Patrik,

    I’m back. Your code works well. Thank you!

    Can I do the same for the profile page? I mean disable profile page of users of role ‘administrator’ and ‘subscriber’, so that their profile can’t be accessed in any way?
    With your code it is still possible to visit user’s profile when knowing their slug.

    UsersWP’s Doc is silent on hook regrettably.

    Hi,

    Try following code and let me know if it works for you or not:

    add_action('template_redirect', 'template_redirect_cb');
    function template_redirect_cb(){
    	global $post;
    
    	if ( ! is_page() ) {
    		return false;
    	}
    
    	if ( uwp_is_page_builder() ) {
    		return false;
    	}
    
    	$current_page_id = isset($post->ID) ? absint($post->ID) : '';
    	$uwp_page = uwp_get_page_id('profile_page', false);
    	if ( $uwp_page && ((int) $uwp_page ==  $current_page_id ) ) {
    		$user = uwp_get_user_by_author_slug();
    		if( ! $user || ! $user->roles ){
    			return false;
    		}
    
    		if(isset($user->roles) && $user->ID != get_current_user_id() && (in_array('administrator', (array) $user->roles) || in_array('subscriber', (array) $user->roles))){
    			wp_redirect( home_url() );
    			exit();
    		}
    	}
    }

    Regards,
    Patrik

    Thread Starter jonny-s

    (@jonny-s)

    Hi Patrik

    your code works fine.

    Thank you a very lot for your fast and accurate answers!

    Great support.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hide users by role’ is closed to new replies.