PHP Functions
Use these in your theme or plugin to show the phone field and check numbers. They use the same settings as the checkout field and work with or without WooCommerce. Check that the plugin is active with function_exists() first.
| Function | What it returns |
|---|---|
nahnu_spf_field( $args ) | HTML for a phone field. Arguments: name, id, label, value, country, required, placeholder, class, wrapper_class, style. Loads the script and styles it needs. |
nahnu_spf_demo( $args ) | HTML for the live demo. Same arguments. |
nahnu_spf_enqueue_assets() | Loads the script and styles on the current page, for when you attach the field yourself with JavaScript. |
nahnu_spf_is_valid( $phone, $country = '' ) | true if the number is valid. false for an empty number. |
nahnu_spf_to_international( $phone, $country = '' ) | The number in international format, such as +447400123456, or an empty string if it isn’t valid. |
nahnu_spf_format( $phone, $country = '' ) | The readable number, such as +1 201-555-0123. |
nahnu_spf_detect_country() | The visitor’s country as a lowercase two-letter code, using your Location settings. Empty if unknown or detection is off. |
nahnu_spf_check( $phone, $country = '' ) | Everything a form needs in one call: valid, empty, international, readable, country and a translated message. |
nahnu_spf_integrations() | The form plugin integrations registry. |
$country is a two-letter code. It’s only needed for numbers typed without a country code.
Show a field#
if ( function_exists( 'nahnu_spf_field' ) ) {
echo nahnu_spf_field( array(
'name' => 'phone',
'required' => true,
) );
}
Check and save a submitted number#
$phone = sanitize_text_field( wp_unslash( $_POST['phone'] ?? '' ) );
$country = sanitize_key( $_POST['phone_country'] ?? '' );
$result = nahnu_spf_check( $phone, $country );
if ( ! $result['valid'] && ! $result['empty'] ) {
// Show $result['message'] to the visitor.
} else {
// Save $result['international'].
}
Convert a number#
echo nahnu_spf_to_international( '07400 123456', 'gb' ); // +447400123456
echo nahnu_spf_format( '+12015550123' ); // +1 201-555-0123