NumCheckr Integration

Validate phone numbers using NumCheckr API with WSMS.

This guide shows how to integrate NumCheckr’s phone number validation service with WSMS to add an extra layer of phone number verification.

Prerequisites

  • Active NumCheckr account
  • NumCheckr API token

Step 1: Create NumCheckr Validation Function

Add this function to your theme’s functions.php or a custom plugin:

function validate_with_numcheckr($mobileNumber) {
    $apiToken = 'YOUR_NUMCHECKR_API_TOKEN';
    $url = 'https://numcheckr.com/api/check-number';

    $response = wp_remote_post($url, [
        'headers' => [
            'Authorization' => 'Bearer ' . $apiToken,
            'Content-Type'  => 'application/json',
            'Accept'        => 'application/json',
        ],
        'body' => json_encode(['phone' => $mobileNumber]),
    ]);

    if (is_wp_error($response)) {
        return new WP_Error('numcheckr_error', 'NumCheckr API request failed');
    }

    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);

    if (isset($data['valid']) && $data['valid'] === true) {
        return true;
    }

    return new WP_Error('invalid_number', 'Invalid mobile number');
}

Step 2: Hook Into WSMS Validation

Connect NumCheckr to WSMS using the wp_sms_mobile_number_validity filter:

add_filter('wp_sms_mobile_number_validity', 'integrate_numcheckr_with_wp_sms', 10, 2);

function integrate_numcheckr_with_wp_sms($isValid, $mobileNumber) {
    // If already invalid, return early
    if (is_wp_error($isValid)) {
        return $isValid;
    }

    // Validate with NumCheckr
    return validate_with_numcheckr($mobileNumber);
}

How It Works

  1. User submits a phone number through WSMS
  2. WSMS runs its default validation
  3. If valid, the number is sent to NumCheckr API
  4. NumCheckr verifies the number and returns the result
  5. Invalid numbers are rejected with an error message

Last updated: December 26, 2024