Halloween Sale 15% off Ends in See the plans
PHP

Require Latin Characters in Addresses

Asks customers to type their name and address in Latin letters, for international carriers whose labels cannot print other alphabets.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_latin_fields = array( 'first_name', 'last_name', 'address_1', 'address_2', 'city' ); // Fields to check.

add_action( 'woocommerce_after_checkout_validation', function ( $data, $errors ) use ( $wpide_latin_fields ) {
    foreach ( array( 'billing', 'shipping' ) as $type ) {
        if ( 'shipping' === $type && empty( $data['ship_to_different_address'] ) ) {
            continue;
        }

        foreach ( $wpide_latin_fields as $field ) {
            $value = (string) ( $data[ $type . '_' . $field ] ?? '' );

            if ( '' !== $value && ! preg_match( '/^[\p{Latin}\p{N}\s\'\-.,\/#&()]+$/u', $value ) ) {
                $errors->add( 'validation', 'Please enter your name and address using Latin letters (A to Z) so the courier can read the label.' );
                return;
            }
        }
    }
}, 10, 2 );

Notes

Accented letters such as é, ü and ø are allowed; edit $wpide_latin_fields to check more or fewer fields.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.