Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active January 6, 2021 02:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/2894109 to your computer and use it in GitHub Desktop.
Save trepmal/2894109 to your computer and use it in GitHub Desktop.
[WordPress Plugin] Simple Registration/Invite Codes
  1. Assumes "Anyone can register" is checked on wp-admin/options-general.php
  2. Create a code by creating a post at wp-admin/edit.php?post_type=invite_codes with the code in the title
  3. 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
  4. When a code is succuessfully used, the code "post" will be marked as 'draft', and post meta will be added indicating who used it.
  5. Disabling the wp_update_post section will allow codes to be reused until manually marked as 'draft'
<?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 );
@gujingc
Copy link

gujingc commented Jul 18, 2019

Hi,

Thank you for posting this!

We are looking for a solution like this. A few questions:

  1. We have implemented our solution with its own registration plugins. Can we insert the code above while keeping the existing registration plugin?
  2. Do we need a separate solution to generate the invitation code, or maybe allow the site admin to set a invitation code?
  3. This nice snippet of code was published 7 years ago. Is there any issue if we use it for the current version of WordPress?

Thank you so much!

Jing

@mvergaraochoa
Copy link

Hi,

This plugin is very useful and I would like to know how to generate bulk of invitation codes instead of doing manually.

Thank you.

Marcelo Vergara

@trepmal
Copy link
Author

trepmal commented Jun 18, 2020

@mvergaraochoa, Using WP-CLI would make bulk importing possible.

For example, you could create a file called codes.txt and put as many invite codes as you want, each on their own line.

Then loop through those codes and use wp post create to import them

Example

Here's my codes.txt file, which contains 3 codes:

$ cat codes.txt 
code1
code2
code3

Then I can loop through that file and create new posts (invite_codes)

$ while IFS=, read -r code; do wp post create --post_type=invite_codes --post_title=$code --post_status=publish; done < codes.txt
Success: Created post 27217.
Success: Created post 27218.
Success: Created post 27219.

@mvergaraochoa
Copy link

Hi Kailey,

Thank you very much for your reply. I will insert the code in my project.

Regards and have a good day.

Marcelo

@mvergaraochoa
Copy link

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

@mvergaraochoa
Copy link

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

@trepmal
Copy link
Author

trepmal commented Jun 20, 2020

@mvergaraochoa,

I've moved this gist to a proper repo, https://github.com/trepmal/simple-invite-codes/, and added a fix for the registration error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment