Limit number of Users registered in a Wordpress website

  • limit-number-of-users-wordpress

Yes, You can limit the number of users in your wordpress website. Wordpress has the flexibility to do anything and everything on the web.

Lets Assume we need users not more than 50, We can simply use the below snippet to limit the number of users to 50. After 50 users reached wordpress registration form will not work.

Lets goto the code snippet straight away, Add the below snippet to your themes function.php

Code

function count_users_wp() {
//Get total no. of users count
    global $wpdb;
    $users_count = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->users}");
    return $users_count;
}

function limit_users_register() {
    $count = apply_filters('limit_user_count',50);
    $users = count_users_wp();
    if ($users >= $count) { //Check if total users exceeds 
        update_option('users_can_register',0);  //Disable registration
    }else{
        update_option('users_can_register',1);  //Enable registration
    }
}
add_action('user_register','limit_users_register');

function limit_user_register($option) {
    remove_filter('pre_option_users_can_register','limit_user_register');
    $reg = get_option('users_can_register');
    if (0 === $reg) {
        return 0; //break
    }
    $count = apply_filters('limit_user_count',50);
    $users = count_users_wp();
    if ($users >= $count) {
        update_option('users_can_register',0); //Disable registration
        return 0;
    } else {
        update_option('users_can_register',1); //proceed to registration
        return $option;
    }
}
add_filter('pre_option_users_can_register','limit_user_register');