Creating a Closed WordPress Community Using Referral Codes

So, first up, we need some sort of submit form. Here’s a simple one, it can be whatever you want, obviously. This is just for this example. Emails the person on submission and sets up a password key for them in the wp_options table.

<?php
add_action( 'init', 'wpse15535_add_shortcode' );
function wpse15535_add_shortcode()
{
    add_shortcode( 'invite-form', 'wpse15535_invite_form' );
}


function wpse15535_invite_form()
{
    ?>
    <form action="" method="post">
        <input type="hidden" name="wpse15535_action" value="send_invite" />
        <input type="text" name="wpse15535_email" />
        <input type="submit" value="<?php _e( 'Send Invite' ); ?>" />
    </form>
    <?php
    // Do some stuff if our action is set.
    if( isset( $_POST['wpse15535_action'] ) && 'send_invite' == $_POST['wpse15535_action'] )
    {
        // Get the email
        $email = isset( $_POST['wpse15535_email'] ) && is_email( $_POST['wpse15535_email'] ) ? $_POST['wpse15535_email'] : false;
        if( ! $email ) return; // bad email? bail.

        // generate a random 30 character string.
        $key = wp_generate_password( 30 );

        // store our keys in the options table
        $opts = get_option( 'wpse15535_keys' );
        $opts = (array) $opts;
        $opts[$email] = $key;
        update_option( 'wpse15535_keys', $opts );

        // Send an email!
        $message = "You're invited to join a sweet community! Click here: " . home_url( 'wp-login.php?action=register&invite_key=' . $key );
        wp_mail( $email, "You're invited!", $message );
    }
}

Then it’s on to the registration form. Make sure “Anyone can register” is checked in the settigns > general options.

The email that gets sent to the person contains a url with the invite key as a URL parameter invite_key. So first, we’ll hook into the registration form and echo out a hidden input with the key if it’s present.

<?php
add_action( 'register_form', 'wpse15535_add_key' );
function wpse15535_add_key()
{   
    if( isset( $_GET['invite_key'] ) )
    {
        echo '<input type="hidden" name="invite_key" value="' . esc_attr( $_GET['invite_key'] ) . '" />';
    }
}

Next, we’ll hook into the registration_errors filter. We need to see if there are any errors already, and bail if there are. Then we see if we even have a key. If not, add an error and message. They we get the option that contains are email => key pairs and see if the entered email matches up with the key we have for it. If not, add and error and return.

If nothing goes wrong, the user should be able to register no problem.

<?php
add_filter( 'registration_errors', 'wpse15535_register_post', 10, 3 );
function wpse15535_register_post( $errors, $login, $email )
{
    // bail if something has gone wrong already as they won't be able to register.
    if( $errors->get_error_codes() ) return $errors;

    // If our key isn't set an error and bail
    if( ! isset( $_REQUEST['invite_key'] ) )
    {       
        $errors->add( 'no_key', __( 'You need an invite code to register for this site' ) );
        return $errors;
    }

    $opts = get_option( 'wpse15535_keys' );

    // see if this email has a key and if it matches what was submitted.
    if( ! isset( $opts[$email] ) || $_REQUEST['invite_key'] != $opts[$email] )
    {
        $errors->add( 'invalid_key', __( 'Invalid Registration Key' ) );
        return $errors;
    }

    // everything okay? Just return the errors and let it go through.
    return $errors;
}

As a plugin: https://gist.github.com/1181933

Leave a Comment