# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Users](https://wpide.com/library/category/users/)
- Tags: registration, invite, signup
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/registration-invite-codes/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Require an Invite Code to Register" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
