# Limit Address Line Length for Shipping Labels

> Stops checkout when an address line is longer than your carrier's label allows (often 35 characters) and asks the customer to use the second line.

- Type: PHP
- Runs: Run everywhere
- Category: [WooCommerce](https://wpide.com/library/category/woocommerce/)
- Requires: WooCommerce
- Tags: woocommerce, checkout, validation, shipping
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/woo-address-length-limit/

## Code

```php
$wpide_address_max = 35; // Maximum characters per address line.

add_filter( 'woocommerce_default_address_fields', function ( $fields ) use ( $wpide_address_max ) {
    foreach ( array( 'address_1', 'address_2', 'company' ) as $key ) {
        if ( isset( $fields[ $key ] ) ) {
            $fields[ $key ]['maxlength'] = $wpide_address_max;
        }
    }

    return $fields;
} );

add_action( 'woocommerce_after_checkout_validation', function ( $data, $errors ) use ( $wpide_address_max ) {
    foreach ( array( 'billing_address_1', 'billing_address_2', 'shipping_address_1', 'shipping_address_2' ) as $key ) {
        if ( ! empty( $data[ $key ] ) && mb_strlen( (string) $data[ $key ] ) > $wpide_address_max ) {
            $errors->add( 'validation', sprintf( 'Address lines can be up to %d characters. Please move the rest to the second address line.', $wpide_address_max ) );
            return;
        }
    }
}, 10, 2 );
```

## Notes

Also sets a maxlength on the fields so customers see the limit while typing.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Limit Address Line Length for Shipping Labels" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
