How to default a zip code and why you should think about doing it

Article Last Updated: October 12, 2017

This article will talk about (1) why using a default zip code value is a good idea, (2) how to set a default zip code, (3) how to remove the regex on the zip code field so any values will work.

Why using a default zip code is a good idea

For address fields, MailChimp requires address 1, city, and zip code. If these three fields are not filled in, MailChimp will return an error (Error with Address field: Please enter a complete address).  We hide the zip code field for all countries except the U.S. and U.K. This means that if you’re trying to subscribe while using an address field and you do not live in the U.S. or U.K., you won’t fill out the zip code field and your subscription may fail!

To get around this issue, you can set a default value for the zip code using our filter: yikes-mailchimp-default-zip-code.

This is ideal for users who also set up a default country that is not the U.S. or U.K. (see our KB article here for setting up a default country). However, even if your default country is the U.S. (which is the plugin’s default), this will help you capture the address of non-U.S. residents and reduce form submission errors.

How to use our filter to default a zip code

Filter: yikes-mailchimp-default-zip-code

Parameters:

  • $default_zip_value – The default value for the zip code field. The default is an empty string '' (i.e. we do not default the zip code field to any value).
  • $form_id – This is the ID of the current form. This will allow you to change the default zip code behavior based on the form (for example, you may have a form for U.S. users and a different form for non-U.S. users. This allows you to filter one of those forms and not the other one).

The above code would default the zip code field to n/a. You could put any non-zero value here you’d like, just replace 'n/a' with the default value you’d like.

If you’re using a default value that is not 5 numeric digits, then there’s one more step you need to take. Defaulting the zip code field to ‘n/a’ will cause an error because we’re expecting (and enforcing) a zip code field to be 5 digits (or 5 digits hyphen 4 digits). But we also provide a filter for that.

To get around the zip code constraints, you can use the filter yikes-mailchimp-zip-pattern.

How to use our filter to remove the zip code regex validation

Filter: yikes-mailchimp-zip-pattern

Parameters:

  • $regex_pattern – The regular expression used to validate the zip code field. The default is  \d{5,5}(-\d{4,4})?.
  • $form_id – The ID of the current form.

Using the regex .* will allow any characters except line terminators (e.g. hitting enter).

And that’s it; enjoy receiving subscriptions from all over the world!