I using CRED form for registration and want to restrict registration only with work email address.
I am using the following code but no value is captured from the email field on form submission.
<?php
/**
* New custom code snippet (replace this with snippet description).
*/
// Put the code of your snippet below this comment.
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
// Put the code of your snippet below this comment.
function employer_registration_validation($error_fields, $form_data) {
// validate specific form
if ( $form_data['id'] == form-name ) {
// Split $field_data into separate $fields and $errors
list( $fields,$errors ) = $error_fields;
error_log('Error field Data is ' . $error_fields);
$employeremail = $_POST['user_email'];
if($employeremail == null) {
$employeremail = $fields['user_email']['value'];
}
error_log('User email entered is ' . $employeremail);
if ( empty( $employeremail ) ) {
// check if endorser email has been entered
$errors['user_email'] = 'You must provide an email';
} else {
$publicemaildomains = array("@gmail", "@protonmail", "@yahoo", "@outlook", "@hotmail", "@live", "@msn", "@passport");
foreach($publicemaildomains as $publicemaildomain ) {
if (strpos($employeremail, $publicemaildomain) !== false) {
$errors['user_email'] = "Please register with a business email address";
error_log('Personal email found');
break;
}
} // end of loop
} // end of else where employer email is not null
return array($fields,$errors);
} // right form
} // right function
add_filter( 'cred_form_validate', 'employer_registration_validation', 10, 2 );
In this code the code finds a personal email (as per error_log) and still does not show error on registration.
I tested a simplified version of what you are aiming for on my own local site and confirmed it worked as intended, you may want to use a variation of this:
function ts_validate_user_form( $error_fields, $form_data )
{
if ( $form_data['id'] == 123 )
{
list($fields, $errors) = $error_fields;
$email_blacklist = array( 'hotmail', 'gmail' );
// validate user email
if ( empty( $fields['user_email']['value'] ) )
{
$errors['user_email'] = "Email address must be provided";
return array( $fields, $errors );
}
foreach ( $email_blacklist as $needle ) {
if ( strpos( $fields['user_email']['value'], $needle ) !== false )
{
$errors['user_email'] = "Please enter a work email address";
return array( $fields, $errors );
}
}
}
return $error_fields;
}
add_filter( 'cred_form_validate', 'ts_validate_user_form', 10, 2 );
I used your code and it did not work for me. Also, in your code, why are you returning fields twice? One for array($fields, $errors) and another for $error_fields
I'm not sure why it isn't working on your site, but you can see the code working on this sandbox site I prepared.
You can test the form on this page: hidden link
(My code example only prohibits hotmail and gmail addresses.)
Once you have checked the form works on the front end you can inspect the back end via this link: hidden link
(There is a form Register Author on the page Register Author, and the code is added at Toolset > Settings > Custom Code.)
There are several return statements because in this scenario there will only ever be one error (if any), so if there is no email address we can stop, if there is a prohibited email address we can stop, and if there are no errors we need to return the unchanged first parameter $error_fields.