settings = Plugin::get_settings( 'users' );
add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] );
if( !empty( $this->settings['disable_user_registration'] ) ) {
add_filter( 'pre_option_users_can_register', [ $this, 'close_registration_option' ] );
add_action( 'login_init', [ $this, 'block_wp_registration_form' ] );
add_filter( 'register_url', [ $this, 'remove_register_url' ] );
}
}
/**
* Adds a custom settings field for disabling user registration to the provided settings fields array.
*
* @param array $fields The array of existing settings fields to which the new field will be added.
*
* @return array The modified array of settings fields including the new field for disabling user registration.
*/
public function adminease_settings_fields( array $fields ): array {
$fields['users']['fields'][] = [
'type' => 'switch',
'id' => 'disable-user-registration',
'name' => 'adminease[users][disable_user_registration]',
'value' => $this->settings['disable_user_registration'] ?? '',
'label_class' => 'pwp-switch',
'input_class' => 'pwp-field-input',
'label' => __( 'Disable user registration', 'adminease' ),
'description' => __( 'Disabling user registration in WordPress using PHP gives you stronger control over who can create accounts on your site. This approach removes access to the default WordPress registration form (wp-login.php?action=register) and answers "Anyone can register" with off without changing the value saved in Settings > General, so turning this off gives your own setting back. Administrators can still create users in wp-admin and through the REST API. This method is ideal for preventing spam and hardening site security.', 'adminease' ),
];
return $fields;
}
/**
* Answers "Anyone can register" with off without writing it to the database, so the saved value comes back when the switch is turned off.
* Left alone on the Settings > General screen, so it still shows (and saves) the value the site has.
*
* @param mixed $pre The value so far (false when nothing has short-circuited the option).
*
* @return mixed
*/
public function close_registration_option( $pre ) {
global $pagenow;
if( is_admin() && in_array( $pagenow, [ 'options-general.php', 'options.php' ], true ) ) {
return $pre;
}
return 0;
}
/**
* Blocks the WordPress registration form by showing an error message
* if the "action" query parameter is set to "register".
* @return void
*/
public function block_wp_registration_form(): void {
if( 'register' === sanitize_key( wp_unslash( $_GET['action'] ?? '' ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
wp_die(
esc_html__( 'User registration is disabled on this site.', 'adminease' ),
esc_html__( 'Registration Disabled', 'adminease' ),
[ 'response' => 403 ]
);
}
}
/**
* Removes the registration URL by returning an empty string.
*
* @param string $url The original registration URL.
*
* @return string Always returns an empty string.
*/
public function remove_register_url( string $url ): string {
return '';
}
}