limit – Limiting the number of users – WordPress Development Stack Exchange

To limit the no. of members on a WordPress single site, use the followig snippet in your theme’s functions.php. {In the code below, 4 is the number of site users (excluding admin)}

function count_reg_users_wpse_110036() {
  global $wpdb;
  $users = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->users}");
  return $users;
}
 
function limit_users_wpse_110036() {
  $count = apply_filters('limit_user_count',4);
  $users = count_reg_users_wpse_110036();
  if ($users >= $count) {
    update_option('users_can_register',0);
  }
}
add_action('user_register','limit_users_wpse_110036');
 
function limit_user_option_wpse_110036($option) {
  remove_filter('pre_option_users_can_register','limit_user_option_wpse_110036');
  $reg = get_option('users_can_register');
  if (0 === $reg) {
    return 0;
  }
  $count = apply_filters('limit_user_count',4);
  $users = count_reg_users_wpse_110036();
  if ($users >= $count) {
    update_option('users_can_register',0);
    return 0;
  } else {
    return $option;
  }
}
add_filter('pre_option_users_can_register','limit_user_option_wpse_110036');

 
via limit – Limiting the number of users – WordPress Development Stack Exchange.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.