How to limit number of users in a site

Hi,

I need to add a limitation on the number of users than can be created for a particular site.

I'm trying to use this function on the functions.php file of one of the sites as an example but it's not working:

function limit_users_register() {
$main_blog_id = 1; //
switch_to_blog( $main_blog_id );
update_option('users_can_register',0); //Disable registration
restore_current_blog(); // Switches back to the original blog
}
add_action('user_register','limit_users_register');

I've read that the user:register function gets executed after a user has been already added. What function can I used instead then?Can you please tell me what's the correct way of doing this?

Thanx!

  • Milan

    Greetings @Pam,

    Welcome to Wpmu-dev.

    I trust you are having a good day and thanks for the posting. :slight_smile:

    Credit: http://wordpress.stackexchange.com/questions/110036/limiting-the-number-of-users

    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) 1 => Yes
    #2) 0 => No

    As usual: There's a filter to intercept that from a plugin.

    "option_{$option_name}"

    So you can simply attach a callback to this filter and check the amount of users with a WP_User_Query for the get_current_blog_id().

    add_filter( 'option_users_can_register', 'limit_total_users' );
    function limit_total_users( $option )
    {
        // Nothing to do here
        if ( 0 === $option )
            return $option;
    
        static $users = null;
        $limit = 50;
    
        if ( null === $users )
        {
            $users_query = new WP_User_Query( array(
                'blog_id' => get_current_blog_id()
            ) );
            $users = $users_query->get_total();
        }
    
        // Abort if we're above the limit
        if ( $limit > $users )
            return 0;
    
        return $option;
    }

    You can add this code snippet in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    The nice thing about this code snippet is, that it doesn't do an additional query if the registration is already turned off.

    I hope this helps you.

    Wish you productive day ahead. :slight_smile:

    Best Regards,
    Milan.

  • Pam

    Hi,

    First of all thanks a lot for your quick and complete answer. However, I've tried the code and it doesn't work. I've copied on the functions.php file of the theme site.

    It seems to me that option_users_can_register function all it does - when set the value to 0 - is to prevent users from registering themselves. But is not preventing that the admin of the site adds as many users from the dashboard as he wants. Am I wrong?

    Thanks!

  • Milan

    Hello @Pam,

    I trust you are having a good day and sorry for being late here. I hope you don't mind. :slight_smile:

    Here is another solution which i've found for you which stops self-user-registration.
    http://www.idiotinside.com/2014/12/03/limit-number-of-users-registered-in-a-wordpress-website/.

    I've tested this on my site and it is working fine. :slight_smile: Solution provided on above link is for single site wordpress installation, but you can easily configure it for specific site in multisite with https://codex.wordpress.org/Function_Reference/get_current_blog_id function's integration.

    And function count_user_up in solution will be like this for multisite.

    function count_users_wp() {
    	//Get total no. of users count based on current blog id
         $users_query = new WP_User_Query( array(
                'blog_id' => get_current_blog_id()
            ) );
    
        return $users_query->get_total();
    }

    It seems to me that option_users_can_register function all it does - when set the value to 0 - is to prevent users from registering themselves. But is not preventing that the admin of the site adds as many users from the dashboard as he wants. Am I wrong?

    No, you are absolutely right, Unfortunately there isn't any out of box solution for available for stopping user creation based on limit criteria in admin panel as wordpress is not providing an hooks prior to user registration on wordpress site.

    But there could be one workaround to this, but it will use much database resources and is not advisable. But even if you want to try out here is how you can do it.

    Wordpress provides hook wpmu new user which fires immediately after a new user is created. You can hook function to this hook and then delete recently added user from database with delete_user functions when your limit is crossed. This way newly created user will never be able to created even if its registered from wordpress admin panel.

    Note:Above solution is not good one, as deleting user just after creating is kind of database intensive task. .

    I hope this helps you. :slight_smile:

    Please let me know if there is anything else I can help you with.

    With Best Regards,
    Milan.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.