I am trying to write a plugin. I have added an extra field in a registration form using the plugin successfully. I would like to check whether the value in the extra field matches any username already registered.
When I try to validate using the predefined usermeta functions. I get the below errors:
require( 'C:\wamp\www\cpa\wp-load.php' )
require_once( 'C:\wamp\www\cpa\wp-config.php' )
require_once( 'C:\wamp\www\cpa\wp-settings.php' )
I am able to do this in a theme template. But I would like to know how to access all the predefined functions of WordPress inside a plugin like we do in theme files.
Code:
//1. Add a new form element...
$referral_username = $_REQUEST['referral'];
add_action('register_form','myplugin_register_form');
function myplugin_register_form (){
$referral_username = ( isset( $_POST['referral_username'] ) ) ? $_POST['referral_username']: '';
?>
<p>
<label for="referral_username"><?php _e('Referral Username','mydomain') ?><br />
<input type="text" name="referral_username" id="referral_username" class="input" size="20"value="<?php echo $referral_username = $_REQUEST['referral']; ?>" size="25" />
</label>
</p>
<?php
}
//2. Add validation. In this case, we make sure referral_username is required.
add_filter('registration_errors', 'myplugin_registration_errors', 10, 3);
function myplugin_registration_errors ($errors, $sanitized_user_login, $user_email) {
if ( empty( $_POST['referral_username'] ) )
$errors->add( 'referral_username_error', __('<strong>ERROR</strong>: You must include a Valid Referral User Name. Else leave it blank.','mydomain') );
return $errors;
}
//3. Finally, save our extra registration user meta.
add_action('user_register', 'myplugin_user_register');
function myplugin_user_register ($user_id) {
if ( isset( $_POST['referral_username'] ) )
update_user_meta($user_id, 'referral_username', $_POST['referral_username']);
}
//Test Usermeta
$user = get_userdatabylogin('vijay');
echo $user->ID; // prints the id of the user;