- Assumes "Anyone can register" is checked on wp-admin/options-general.php
- Create a code by creating a post at wp-admin/edit.php?post_type=invite_codes with the code in the title
- When a user registers at wp-login.php?action=register, they must provide a valid code. Code validation is fully dependent on
get_page_by_title, so there is some loose matching - When a code is succuessfully used, the code "post" will be marked as 'draft', and post meta will be added indicating who used it.
- Disabling the
wp_update_postsection will allow codes to be reused until manually marked as 'draft'
-
-
Save trepmal/2894109 to your computer and use it in GitHub Desktop.
| <?php | |
| /* | |
| Plugin Name: Simple Registration/Invite Codes | |
| Description: Create invite codes to limit registration. Coded quickly, very simple, limited real-world testing | |
| Author: Kailey Lampert | |
| Author URI: http://kaileylampert.com/ | |
| */ | |
| /** | |
| * Register post type | |
| */ | |
| function ic_cpt() { | |
| register_post_type( 'invite_codes', array( | |
| 'label' => 'Codes', | |
| 'public' => false, | |
| 'show_ui' => true, | |
| 'supports' => array( 'title', 'custom-fields' ), | |
| ) ); | |
| } | |
| add_action( 'init', 'ic_cpt' ); | |
| /** | |
| * Markup for registration form field | |
| */ | |
| function ic_add_register_field() { | |
| ?> | |
| <p> | |
| <label for="invite_code"><?php echo esc_html( 'Invite Code' ); ?></label> | |
| <input type="text" name="invite_code" id="invite_code" class="input" size="25" /> | |
| </p> | |
| <?php | |
| } | |
| add_action( 'register_form', 'ic_add_register_field' ); | |
| /** | |
| * Verify invite code | |
| */ | |
| function ic_add_register_field_validate( $sanitized_user_login, $user_email, $errors) { | |
| // if code missing | |
| if ( ! isset( $_POST['invite_code'] ) || empty( $_POST['invite_code'] ) ) { | |
| return $errors->add( 'nocode', '<strong>Sorry</strong>, membership by invitation only.' ); | |
| } | |
| $given_code = sanitize_text_field( wpautop( $_POST['invite_code'] ) ); | |
| // check if exists. Loose matching | |
| $is_valid_code = get_page_by_title( $given_code, OBJECT, 'invite_codes' ); | |
| // if doesn't exist, or has been used already | |
| if ( is_null( $is_valid_code ) || 'draft' == $is_valid_code->post_status ) { | |
| return $errors->add( 'invalidcode', '<strong>ERROR</strong>: You provided an invalid code.' ); | |
| // stricter matching of code (e.g. make it case-sensitive) | |
| } elseif ( $is_valid_code->post_title !== $given_code ) { | |
| return $errors->add( 'invalidcode', '<strong>ERROR</strong>: You provided an invalid code.' ); | |
| // match | |
| } else { | |
| // if valid, mark as used by setting status to draft | |
| // this can be disabled to allow reuse | |
| wp_update_post( array( | |
| 'ID' => $is_valid_code->ID, | |
| 'post_status' => 'draft' | |
| ) ); | |
| // and record who used it in meta | |
| add_post_meta( $is_valid_code->ID, 'used_by', wp_json_encode( [ time(), $sanitized_user_login] ) ); | |
| } | |
| } | |
| add_action( 'register_post', 'ic_add_register_field_validate', 10, 3 ); |
Hello Kailey,
Finally, I used this php to generate random invitation codes :
// Generate invitation codes and store this codes in order meta fields
for ($x = 1; $x <= $qty_new; $x++) {
$inv_code = inv_code(8);
wp_insert_post(array(
'post_type' => 'invite_codes',
'post_title' => $inv_code,
'post_status' => ‘publish’ ));
}
function inv_code($length) {
$characters = '0123456789abcdefghijklmnopqrs092u3tuvwxyzaskdhfhf9882323ABCDEFGHIJKLMNksadf9044OPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
Regards,
Marcelo
Hello Kailey:
I found a problem with the plugin code related to email validation process after user registration:
When you write an invalid email in registration form and use a valid invitation code, it shows the WordPress error related to email field but the invitation code status marks as "draft" and it no longer be available.
How can it be avoided to mark the invitation code status as draft if there is a email validation error like mentioned before?
Regards,
Marcelo
I've moved this gist to a proper repo, https://github.com/trepmal/simple-invite-codes/, and added a fix for the registration error.
Hi Kailey,
Thank you very much for your reply. I will insert the code in my project.
Regards and have a good day.
Marcelo