Require an Invite Code to Register
Adds an "Invite code" field to the registration form and only lets people sign up with one of your codes, each with an optional limit on how many times it can be used.
Opens WPIDE on
Code
// Code => maximum number of sign-ups (0 = unlimited).
$wpide_invite_codes = array(
'WELCOME2026' => 0,
'VIP-TEAM' => 25,
);
$wpide_find_code = function ( $entered ) use ( $wpide_invite_codes ) {
foreach ( $wpide_invite_codes as $code => $max ) {
if ( hash_equals( strtoupper( $code ), strtoupper( $entered ) ) ) {
return $code;
}
}
return '';
};
add_action( 'register_form', function () {
$value = isset( $_POST['wpide_invite'] ) ? sanitize_text_field( wp_unslash( $_POST['wpide_invite'] ) ) : '';
printf( '<p><label for="wpide_invite">Invite code</label><input type="text" name="wpide_invite" id="wpide_invite" class="input" value="%s" autocomplete="off" required></p>', esc_attr( $value ) );
} );
add_filter( 'registration_errors', function ( $errors ) use ( $wpide_invite_codes, $wpide_find_code ) {
$entered = isset( $_POST['wpide_invite'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['wpide_invite'] ) ) ) : '';
$code = '' !== $entered ? $wpide_find_code( $entered ) : '';
if ( ! $code ) {
$errors->add( 'wpide_invite', '<strong>Error:</strong> Please enter a valid invite code.' );
return $errors;
}
$uses = (array) get_option( 'wpide_invite_code_uses', array() );
if ( $wpide_invite_codes[ $code ] > 0 && ( $uses[ $code ] ?? 0 ) >= $wpide_invite_codes[ $code ] ) {
$errors->add( 'wpide_invite_used', '<strong>Error:</strong> This invite code has been used up.' );
}
return $errors;
} );
add_action( 'register_new_user', function ( $user_id ) use ( $wpide_find_code ) {
$code = isset( $_POST['wpide_invite'] ) ? $wpide_find_code( trim( sanitize_text_field( wp_unslash( $_POST['wpide_invite'] ) ) ) ) : '';
if ( $code ) {
$uses = (array) get_option( 'wpide_invite_code_uses', array() );
$uses[ $code ] = ( $uses[ $code ] ?? 0 ) + 1;
update_option( 'wpide_invite_code_uses', $uses, false );
update_user_meta( $user_id, 'wpide_invite_code', $code );
}
} );
Notes
Codes are not case-sensitive. Use 0 for unlimited uses; usage counts are kept in the wpide_invite_code_uses option.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.