Set a default zip code - Code Parrots

Knowledge Base

Set a default zip code

Article Last Updated: March 4, 2021

This article covers why using a default zip code value is a good idea, how to set a default zip code and how to remove the regex on the zip code field so any value will work.

Why using a default zip code?

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).
/**
* Default the zip code to 'n/a'.
*
* @param string | $default_zip_value| The default value for the zip code
* @param int    | $form_id     | The ID of the current form
*/
function yikes_mailchimp_default_zip_code( $default_zip_value, $form_id ) {
return 'n/a';
}

add_filter( 'yikes-mailchimp-default-zip-code', 'yikes_mailchimp_default_zip_code', 10, 2 );

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.
/**
* Change the zip code regular expression to allow any characters.
*
* @param string | $regex_pattern| The regular expression used to validate the zip code
* @param int    | $form_id | The ID of the current form
*/
function yikes_mailchimp_remove_zip_code_validation( $regex_pattern, $form_id ) {
return '.*';
}

add_filter( 'yikes-mailchimp-zip-pattern', 'yikes_mailchimp_remove_zip_code_validation', 10, 2 );

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!