On a recent project a client asked us to setup address validation so that users cannot use PO Boxes. If you want to validate the shipping or billing address before account creation in WooCommerce, and block P.O. Boxes, the easiest way is with a code snippet or a plugin that hooks into the account registration and checkout process.

Here’s the simplest way to block P.O. Boxes using a code snippet:

// functions.php or a code snippets plugin
add_action(‘woocommerce_checkout_process’, ‘ts_block_po_box_checkout’);
add_action(‘woocommerce_register_post’, ‘ts_block_po_box_registration’, 10, 3);

function ts_contains_po_box($address) {
return preg_match(‘/P.?\sO.?\sBox/i’, $address);
}

function ts_block_po_box_checkout() {
$billing = $_POST[‘billing_address_1’] ?? ”;
$shipping = $_POST[‘shipping_address_1’] ?? ”;

if (ts_contains_po_box($billing)) {
    wc_add_notice(__('Sorry, we do not ship to P.O. Boxes. Please enter a valid street address.'), 'error');
}

if (ts_contains_po_box($shipping)) {
    wc_add_notice(__('Sorry, we do not ship to P.O. Boxes. Please enter a valid street address.'), 'error');
}

}

function ts_block_po_box_registration($username, $email, $errors) {
$address = $_POST[‘billing_address_1’] ?? ”;

if (ts_contains_po_box($address)) {
    $errors->add('po_box_blocked', __('We do not allow P.O. Box addresses for registration.'));
}

}

How it works:

  • Runs on checkout and registration
  • Checks if the address includes variations of “P.O. Box”
  • If it does, it prevents the process and shows an error

You can add this to your child theme’s functions.php, or better yet, use the Code Snippets plugin (way easier and safer).


🔌 Option 2: Use a Plugin

If you prefer a no-code method:


✅ Recommended Combo

For the easiest and most flexible solution:

  • Use the code snippet above for basic PO Box blocking
  • Add WooCommerce Checkout Field Editor or Address Validation plugin if you want better UX (like autocomplete, formatting, etc.)