Integrations
Validate phone numbers and emails in WordPress
Validate emails and phone numbers in Contact Form 7 with a small plugin using wp_remote_post. Show field errors, cache results and fail open on errors.
Updated 2026-11-23
This recipe adds a small plugin that checks the email and phone fields of a Contact Form 7 form against the API when a visitor submits it. A bad address or number shows an error under the field, and an API problem never blocks the form.
What you need
- WordPress 6 or later on PHP 8.0 or later, and Contact Form 7 installed.
- A Qelka API key. A
sk_test_key returns simulated results and never spends credits: see Test mode. - Access to
wp-config.phpor your server's environment variables, and the ability to upload a plugin. Some managed hosts restrict custom plugins or outgoing requests on entry plans, so check your plan.
Steps
1. Store the key
In wp-config.php, above the line that says to stop editing, add:
define('VALIDATION_API_KEY', getenv('API_KEY') ?: '');
Then set API_KEY in your host's environment settings. If your host has no environment variables, replace the call with the key itself in wp-config.php, which is outside the web root of most installs and never rendered in a page. Never put the key in a theme file or in the database.
2. The plugin
Create wp-content/plugins/signup-validation/signup-validation.php:
<?php
/**
* Plugin Name: Signup validation
* Description: Checks emails and phone numbers of Contact Form 7 forms.
* Version: 1.0.0
*/
if (!defined('ABSPATH')) {
exit;
}
function signup_validation_call(string $path, array $body): ?array
{
$key = defined('VALIDATION_API_KEY') ? VALIDATION_API_KEY : getenv('API_KEY');
if (!$key) {
return null;
}
$cache_key = 'sv_' . md5($path . wp_json_encode($body));
$cached = get_transient($cache_key);
if (is_array($cached)) {
return $cached;
}
for ($attempt = 1; $attempt <= 3; $attempt++) {
$response = wp_remote_post('https://api.qelka.com/v1' . $path, [
'timeout' => 8,
'headers' => [
'Authorization' => 'Bearer ' . $key,
'Content-Type' => 'application/json',
],
'body' => wp_json_encode($body),
]);
if (is_wp_error($response)) {
error_log('validation skipped: ' . $response->get_error_message());
return null;
}
$status = (int) wp_remote_retrieve_response_code($response);
$data = json_decode(wp_remote_retrieve_body($response), true);
if ($status >= 200 && $status < 300 && is_array($data)) {
set_transient($cache_key, $data, DAY_IN_SECONDS);
return $data;
}
if (in_array($status, [429, 500, 502, 503], true) && $attempt < 3) {
$wait = (int) wp_remote_retrieve_header($response, 'retry-after');
sleep(min($wait > 0 ? $wait : $attempt, 5));
continue;
}
$type = $data['error']['type'] ?? 'unknown';
$request_id = wp_remote_retrieve_header($response, 'x-request-id');
error_log("validation skipped: {$type} status={$status} request_id={$request_id}");
return null;
}
return null;
}
function signup_validation_email($result, $tag)
{
$name = $tag->name;
$value = isset($_POST[$name]) ? sanitize_email(wp_unslash($_POST[$name])) : '';
if ($value === '') {
return $result;
}
$check = signup_validation_call('/email', ['email' => $value]);
if ($check && $check['status'] === 'undeliverable') {
$message = $check['did_you_mean']
? sprintf('Did you mean %s?', $check['did_you_mean'])
: 'This email address cannot receive mail.';
$result->invalidate($tag, $message);
}
return $result;
}
add_filter('wpcf7_validate_email', 'signup_validation_email', 20, 2);
add_filter('wpcf7_validate_email*', 'signup_validation_email', 20, 2);
function signup_validation_phone($result, $tag)
{
$name = $tag->name;
$value = isset($_POST[$name]) ? sanitize_text_field(wp_unslash($_POST[$name])) : '';
if ($value === '') {
return $result;
}
$body = ['phone' => $value];
if (isset($_POST['country'])) {
$body['country'] = strtoupper(sanitize_text_field(wp_unslash($_POST['country'])));
}
$check = signup_validation_call('/phone', $body);
if ($check && $check['status'] === 'invalid') {
$result->invalidate($tag, 'This phone number is not valid.');
} elseif ($check && $check['status'] === 'unknown') {
$result->invalidate($tag, 'Please enter the number with its country code, starting with +.');
}
return $result;
}
add_filter('wpcf7_validate_tel', 'signup_validation_phone', 20, 2);
add_filter('wpcf7_validate_tel*', 'signup_validation_phone', 20, 2);
Activate the plugin in the admin. Every Contact Form 7 email and tel field is now checked on submit. The function returns null when the check could not complete, so an outage, a timeout or an empty balance lets the form through. Results are cached for a day with a transient, so a visitor who resubmits does not spend a second credit. The cache key is derived from the value, which is not stored in clear.
3. Add a country field if you need one
A phone number written without + has no meaning until the API knows the country, and the API answers unknown. Either ask for the international format, as the plugin's message does, or add a field named country to the form with two-letter codes such as US or GB, which the plugin forwards. See the phone reference.
4. Read the result
| Result | What the plugin does |
|---|---|
Email undeliverable |
Error under the field, with did_you_mean when there is one |
Email risky or unknown |
Accepts: confirm by email instead |
Phone valid |
Accepts |
Phone invalid |
Error under the field |
Phone unknown |
Asks for the number with its country code |
| Any API failure | Accepts and writes the error type to the PHP log |
To also block disposable inboxes, add || ($check['status'] === 'risky' && $check['reason'] === 'disposable') to the email condition: see Block disposable email signups.
Handle errors and rate limits
Nothing is billed on errors. 401 invalid_api_key and 403 forbidden are key problems, and 402 insufficient_credits is an empty balance: they land in the PHP error log with the request id, so watch it. A 429 waits Retry-After seconds, up to five, then fails open. A key allows 200 requests per 10 seconds. A spam bot can submit your form at that pace, so keep a CAPTCHA or honeypot in front of the check. See Errors and Rate limits.
Related
Frequently asked questions
Which form plugin does this recipe cover?
Plugin or functions.php?
Where do I put the API key in WordPress?
Put this check in your product
Get an API key in under a minute. 100 free credits every month, no card required.
- 100 free credits a month
- No card
- Test keys spend nothing
- Cancel any time