Created
November 14, 2025 20:57
-
-
Save LittleMikeNZ/52180cfc574fd4ec9d9a39d605d56c89 to your computer and use it in GitHub Desktop.
Validate Wordpress Elementor form field to prevent unwanted string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Prevent Auckland being entered as a suburb on the booking form | |
| * | |
| * @return void | |
| */ | |
| function prevent_auckland_submission( $record, $ajax_handler ) { | |
| // Only apply to the specific form ID | |
| $allowed_form_ids = [ | |
| 'booking_form', | |
| 'booking_form_mobile' | |
| ]; | |
| // Get form ID and check if it's in the allowed list - Don't change this | |
| $form_id = $record->get_form_settings( 'form_id' ); | |
| if ( !in_array( $form_id, $allowed_form_ids ) ) { | |
| return; // Exit if the form ID is not in the allowed list | |
| } | |
| $fields = $record->get_field( [ | |
| 'id' => 'suburb', // Matches your form field's key | |
| ] ); | |
| if ( empty( $fields ) ) { | |
| return; | |
| } | |
| $field = current( $fields ); | |
| // Case-insensitive exact match check | |
| if ( strcasecmp( $field['value'], 'Auckland' ) === 0 ) { | |
| $ajax_handler->add_error( | |
| $field['id'], | |
| esc_html__( 'Auckland is not a valid suburb. Please enter your actual suburb.', 'textdomain' ) | |
| ); | |
| } | |
| } | |
| add_action( 'elementor_pro/forms/validation', 'prevent_auckland_submission', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment