Integrating WP SMS Mobile Validity with NumCheckr

Updated on Aug 12, 2024

NumCheckr is a service that provides advanced validation and information about phone numbers. Integrating NumCheckr with WP SMS enhances the validity check process by using NumCheckr’s API to verify phone numbers.

Prerequisites
  • A NumCheckr account and API token.
Integration Steps

Add NumCheckr API Integration Code

First, create a function to call the NumCheckr API and validate the phone number:

function validate_with_numcheckr($mobileNumber) {
    $apiToken = '<YOUR-API-TOKEN-SECRET-VALUE>';
    $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', 'wp-sms'));
    }

    $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 according to NumCheckr', 'wp-sms'));
}

Modify the Mobile Validity Check with NumCheckr

Use the wp_sms_mobile_number_validity filter to integrate NumCheckr:

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

function integrate_numcheckr_with_wp_sms($isValid, $mobileNumber) {
    if (is_wp_error($isValid)) {
        return $isValid;
    }

    $numcheckr_validation = validate_with_numcheckr($mobileNumber);

    if (is_wp_error($numcheckr_validation)) {
        return $numcheckr_validation;
    }

    return $isValid;
}
Related Content

To understand the basics of WP SMS mobile validity check and how to modify it using hooks, refer to our Mobile Number Validity Check in WP SMS document.

Halloween Treat!

Get 10% Off All Plans

Halloween Treat!

Get 10% Off All Plans

Ends Oct 31!