Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

There are a lot of WP plugins for referrals and different ones for generating registration codes for restricted registration pages, but I'm looking for a hybrid.

So, my WP-powered community is completely closed and you can only get in by being invited by other members. Just like Gmail in the beginning.

Best would be if a registered user could just fill in an email and that address would receive a registration link with a unique one-time registration code. Once that referral / new user would have registered I would like to display by which user this person was invited.

Unfortunately I haven't found any suitable plugins for this, even though it sounds like a quite standard situation.

Is there anyone who can give me some advice? Help is appreciated.

share|improve this question
add comment

1 Answer

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.