Skip to content

Instantly share code, notes, and snippets.

@LittleMikeNZ
Created November 14, 2025 20:57
Show Gist options
  • Select an option

  • Save LittleMikeNZ/52180cfc574fd4ec9d9a39d605d56c89 to your computer and use it in GitHub Desktop.

Select an option

Save LittleMikeNZ/52180cfc574fd4ec9d9a39d605d56c89 to your computer and use it in GitHub Desktop.
Validate Wordpress Elementor form field to prevent unwanted string
/**
* 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