Skip to content

Instantly share code, notes, and snippets.

@nkb-bd
Last active January 9, 2024 08:34
Show Gist options
  • Save nkb-bd/d15564d0af943d1091a1904edc0f4c85 to your computer and use it in GitHub Desktop.
Save nkb-bd/d15564d0af943d1091a1904edc0f4c85 to your computer and use it in GitHub Desktop.
Email Validation using wp fluent form
add_filter('fluentform/validate_input_item_input_email', function ($default, $field, $formData, $fields, $form) {
// You may change the following 3 lines
$targetFormIds = [140,141];
$errorMessage = 'Looks like email is not correct'; // You may change here
$emailableApiKey = 'live_b67c9cb0585e27dd256c';
if (!in_array($form->id, $targetFormIds)){
return $default;
}
$fieldName = $field['name'];
if (empty($formData[$fieldName])) {
return $default;
}
$email = $formData[$fieldName];
$request = wp_remote_get('https://api.emailable.com/v1/verify?email='.$email.'&api_key='.$emailableApiKey);
if(is_wp_error($request)) {
return $default; // request failed so we are skipping validation
}
$response = wp_remote_retrieve_body($request);
$response = json_decode($response, true);
if($response['state'] == 'deliverable') {
return $default;
}
return $errorMessage;
}, 10, 5);
@aadrshkashyp
Copy link

I've identified a syntax error in the PHP code snippet provided in the issue. The error is on the line:

if (!in_array(form->id, targetFormIds)){

This line is attempting to use the -> operator with a non-object, causing the syntax error. In PHP, the -> operator is used to access properties or methods of an object. However, form appears to be a variable and should be accessed with the $ sign. Furthermore, if form is an associative array, the correct way to access its elements would be using the array syntax.

The corrected line should be:

if (!in_array($form['id'], $targetFormIds)){

@nkb-bd
Copy link
Author

nkb-bd commented Jan 9, 2024

Thanks I have updated it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment