Hello there. Recently I've got an email with interesting request. Basically the website owner would like to automatically disable registration as soon as number of users exceed some predefined limit (in our case it will be 50 users).
By default WordPress core does not have the ability to do so, however this the help of very simple callback method and Access Policy you can accomplish this at no time.
Note! Keep in mind that this example does not take in consideration user roles, so basically it check only the total number of users. The functionality can be easily extended to cover your needed. Check WordPress core function count_users for more information.
Note! The entire functionality is based on the idea that new user can be registered only when WordPress core option users_can_register
option is set so. This is how WordPress core determines if user can register or not with the default /wp-login.php?action=register
endpoint. If you are using third-party solution for the user registration, you have to consult with its developers to make sure that this check is implemented (which it should be based on how WordPress core works).
Step #1. Create a new plugin by adding /wp-content/plugins/extend-aam-policy.php
file with following code:
<?php
/**
* Plugin Name: Extend AAM Policy
* Description: Collection of hooks that extend AAM Policy functionality
* Version: 0.0.1
* Author: Vasyl Martyniuk <vasyl@vasyltech.com>
* Author URI: https://vasyltech.com
*
* -------
* LICENSE: GNU General Public License
*
*/
namespace ExtendedAAMPolicy;
use AAM,
AAM_Core_API;
/**
* Main plugin's class
*
* @package ExtendedAAMPolicy
* @author Vasyl Martyniuk <vasyl@vasyltech.com>
*/
class Plugin {
/**
* Instance of itself
*
* @var ExntededAAMPolicy\Plugin
*
* @access protected
* @static
*/
protected static $instance = null;
/**
* Constructor
*/
protected function __construct() {
add_filter(
'pre_option_users_can_register',
array($this, 'registrationIsAllowed')
);
}
/**
* Check if registration is allowed
*
* Based on the total number of users, determine if registration is allowed on
* not
*
* @param bool $isAllowed
*
* @return bool
*
* @access public
*/
public function registrationIsAllowed($isAllowed) {
static $userCount = null;
// Initialize the total number of users
if (is_null($userCount)) {
$total = count_users();
$userCount = $total['total_users'];
}
$limit = AAM::api()->getAccessPolicyManager()->getParam('RegistrationLimit');
if ($limit !== null) {
// Make sure that $isAllowed contains integer value to bypass the default
// false value
$isAllowed = ($limit > $userCount) ? 1 : 0;
}
return $isAllowed;
}
/**
* Activation hook
*
* @return void
*
* @access public
*/
public static function activation() {
global $wp_version;
//check PHP Version
if (version_compare(PHP_VERSION, '5.4.0') === -1) {
exit(__('PHP 5.4.0 or higher is required.'));
} elseif (version_compare($wp_version, '4.8') === -1) {
exit(__('WP 4.8 or higher is required.'));
} elseif(!defined('AAM_KEY')) {
exit(__('AAM Plugin is required'));
} elseif(version_compare(AAM_VERSION, '6.0.0') === -1) {
exit(__('AAM Plugin version 6.0.0 or higher is required'));
}
}
/**
* Bootstrap the plugin
*
* @return void
*
* @access public
* @static
*/
public static function bootstrap() {
if (is_null(self::$instance)) {
self::$instance = new self;
}
}
}
if (defined('ABSPATH')) {
//activation hooks
register_activation_hook(__FILE__, 'ExtendedAAMPolicy\Plugin::activation');
//bootstrap the plugin
add_action('init', 'ExtendedAAMPolicy\Plugin::bootstrap');
}
Step #2. Define access policy that specifies number of allowed users in the system:
{
"Version": "1.0.0",
"Dependency": {
"wordpress": ">=5.1",
"advanced-access-manager": ">=5.9.1.1"
},
"Param": [
{
"Key": "RegistrationLimit",
"Value": 50
}
]
}
Step #3. Attach defined policy to visitors and you good to go.