0

I have a custom registration form on my homepage and I want to add a field where a code has to be put in to allow people to register. So that only people i give the code to can register.

I believe it'd be as simple as adding a 1+2= ? validation to the form, but i can't even find any info on that.

Let me know if anyone has any ideas. Thanks.

<form action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post"> 

            <input type="text" name="user_login" id="user_login" class="input"  onfocus="if(this.value == 'Username'){this.value = '';}" type="text" onblur="if(this.value == ''){this.value='Username';}" value="Username" />


            <input type="text" name="user_email" id="user_email" class="input"  onfocus="if(this.value == 'Email Address'){this.value = '';}" type="text" onblur="if(this.value == ''){this.value='Email Address';}" value="Email Address" />

                <?php do_action('register_form'); ?>  
                <input type="submit" value="Register" id="register" />  

            <p class="statement">A password will be e-mailed to you.</p>  


            </form> 
0

Add a field on your registration form

<label for="secret_reg_code">Secret Code: </label>
<input type="text" name="secret_reg_code" id="secret_reg_code" value="" />

then in your plugin file or in function.php add

define('MY_SECRET_REG_CODE', 'fdsfgadsfgdf');

function check_for_reg_secret_code($errors, $sanitized_user_login, $user_email) {
  if (
    defined('MY_SECRET_REG_CODE') &&
    trim($_POST['secret_reg_code']) != MY_SECRET_REG_CODE
  ) {
    $errors->add(
      'secret_code_error',
      __('<strong>ERROR</strong>: Enter a valid code postal code.', 'your_text_domain')
    );
  }
  return $errors;
}
add_filter('registration_errors', 'check_for_reg_secret_code', 10, 3);

A more 'flexible' way is use WP Settings Api to create an option 'secret_reg_code' where to store the secret code and then the function became:

function check_for_reg_secret_code($errors, $sanitized_user_login, $user_email) {
  $code = get_option('secret_reg_code');
  if ( $code && trim($_POST['secret_reg_code']) != $code) {
    ...

In this way is easy change code periodically.

For more details see Codex for registration_errors filter hook

Edit

To add the field on the standard registration form is possible to use the register_form hook:

function add_secret_code_registration_field() {
    ?>
    <p>
    <label for="secret_reg_code">Secret Code<br />
    <input type="text" name="secret_reg_code" id="secret_reg_code" class="input" value="" size="25" /></label>
    </p>
    <?php
}
add_action('register_form','add_secret_code_registration_field');
| improve this answer | |
  • So this works on the custom register form, how would I go about adding the field to the wp-login.php register form – OneEightLeft Sep 2 '13 at 19:22
  • @OneEightLeft see the edit – gmazzap Sep 2 '13 at 20:00
  • so it worked before I added the field to my wp-login page. Now it doesn't work in my index.php and redirects to my wp-login then i put the code in there and it works. – OneEightLeft Sep 2 '13 at 20:11
  • if you want to see it in action i've pushed it live to www.kevinmonahan.com/test/ and the invite code 238m3d3vac – OneEightLeft Sep 2 '13 at 20:13
  • In your custom login form you manually add the input field for secret code? Once you have do_action('register_form'); in your custom login form, then if you put the add_secret_code_registration_field in your functions.php you do not need to add the field in the custom form: the action will do it for you. And it should works for both standard wp-login and for your custom login. – gmazzap Sep 2 '13 at 20:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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