I was wondering if there is a way to actually limit the amount of users that can register to a WordPress site?
As an example, I am wanting to create a small site that will hold information for a customer, where they can only have a maximum of (for example) 50 users. The idea is that you will need to log in to view the site and only a set amount of users are allowed to be created. Maybe somebody knows of a plugin that might do this?
As you can see in the WordPress Option Reference, there’s an option called
users_can_register
. You can – per default – set it in your (network-)sites settings.1
=> Yes0
=> NoAs usual: There’s a filter to intercept that from a plugin.
So you can simply attach a callback to this filter and check the amount of users with a
WP_User_Query
for theget_current_blog_id()
.The nice thing about this mini plugin is, that it doesn’t do an additional query if the registration is already turned off.
Piggybacking off Kaiser’s answer:
Interesting bit of info on that
count_users()
function is it actually returns an array with a count for each role. You may wish to identify which role you are limiting, or simply add/subtract to account for the difference in, for example, the number of admins – as you may not wish to count yourself in the total.This solution differs from the others in that the primary work in done on user registration. After a user registers the total users are counted and the
users_can_register
option is updated if necessary. That seems to be the best way to minimize front-end processing time. A related function hooked topre_option_users_can_register
prevents blog admins from cheating by toggling the switch in “General Settings” and getting an extra user thereby. Technically only that function is necessary but I have explained the reasons for the other function.