Introduction
Would you like to deny specific phone numbers from submitting entries on your forms? You can easily create a small PHP snippet that will block the form from submitting if the phone number is found on the denial list. In this tutorial, we’ll walk you through the steps on how to achieve this.
Creating your form
First, you’ll need to begin by creating a new form and adding your fields to this form which will include a Phone form field.
In our tutorial, we’re going to use the Smart format for our Phone field.
If you need assistance in creating your form, please see this documentation.
Denying specific phone numbers
In order to deny specific phone numbers, you’ll need to add this snippet to your site.
If you need help in how to add snippets to your site, please see this tutorial.
/* * Deny specific phone numbers from your WPForms. * * @link https://wpforms.com/developers/how-to-deny-specific-phone-numbers-from-submitting/ */ function wpf_dev_blacklist_phone( $field_id, $field_submit, $form_data ) { // List each number in this array and separate with a comma using one of the formats detailed below // Smart Phone Format - +12025550164 // US Phone Format - (202) 555-0164 // International Phone Format - 2025550164 // Optional, you can limit to specific forms. Below, we restrict output to form ID 5 // Remove this section if you wish to run on all forms if ( absint( $form_data[ 'id' ] ) !== 5 ) { return; } // Add your phone numbers separated by a comma $phone_blacklist = array( '+12025550164', ); if( in_array( $field_submit, $phone_blacklist ) ) { wpforms()->process->errors[ $form_data[ 'id' ] ][ $field_id ] = esc_html__( 'Phone not accepted!', 'wpforms' ); return; } } add_action('wpforms_process_validate_phone', 'wpf_dev_blacklist_phone', 10, 3 );
This snippet will only process on the form ID 5. You’ll need to update this ID to match your own form ID. If you need help in finding your ID, please review this helpful guide.
The $phone_blacklist is a list of numbers that you will create which will stop the form from submitting if one of the phone numbers entered on the form matches a number in this list.
The above snippet is based off using the Smart Format for the Phone field. Please see the comments in the snippet above to learn how to format the array if using the US or International Format.
And that’s all you need! Would you like to also turn the phone number into a link inside the email notification? Take a look at our tutorial on How to Make Phone Numbers a Link in Email Notifications.
Related
Action Reference: wpforms_process_validate_phone