I have modified ABT's code above, and made a few improvements:
- It looks for the email field by GF type, instead of label.
- When it finds the type, it verifies that it is a proper email address.
- It verifies that the email matches the confirmation value.
- It verifies that the "username" (re: email) is not already registered.
It seems that my validation message for #4 isn't being displayed (GF is overriding it with "Username is already registered"), but it serves my purpose, so I haven't investigated why.
The setup of my form in GF is as follows:
- no username field
- email address field with confirmation enabled
- user registration: create user maps both username + email address to the email form field
I hope anyone who stumbles upon this finds it useful. I imagine that a LOT of people are trying to do this. GF should really add a solution to the documentation!
// allow user to register with email address by stripping validation errors on
// the email address, so we need to verify that it meets the following conditions:
// 1. Proper email address
// 2. Email address matches confirmation value
// 3. Email address is not already registered
add_filter( 'gform_user_registration_validation', 'custom_filter_registration', 10, 3 );
function custom_filter_registration( $form, $config, $pagenum )
{
foreach( $form['fields'] as $k => $field )
{
if( strtolower($field['type']) == 'email' )
{
// get email address + confirmation value
$id = $field['id'];
$email_1 = strtolower($_POST['input_' . $id]);
$email_2 = strtolower($_POST['input_' . $id . '_2']);
// verify: proper email, and email matches confirmation
if( is_valid_email($email_1) && $email_1 == $email_2 )
{
// verify: email is not already used
if( !function_exists('username_exists') )
require_once(ABSPATH . WPINC . "/registration.php");
if( username_exists($email_1) )
{
$field['failed_validation'] = true;
$field['validation_message'] = 'The email ' . $email_1 . ' is already registered.';
} else {
// OK: remove validation errors
$field['failed_validation'] = false;
$field['validation_message'] = '';
$form['fields'][ $k ] = $field;
}
}
}
}
return $form;
}
function is_valid_email($email) {
return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/i", $email);
}
Posted 11 years ago on Wednesday July 3, 2013 |
Permalink