wc_format_postcode

Format the postcode according to the country and length of the postcode.

Description

(string) wc_format_postcode( (string) $postcode, (number) $country ); 

Returns (string)

Formatted postcode.

Parameters (2)

0. $postcode (string)
The postcode.
1. $country (number)
The country.

Usage

  1. if ( !function_exists( 'wc_format_postcode' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-formatting-functions.php'; 
  3.  
  4. // The postcode. 
  5. $postcode = ''; 
  6.  
  7. // The country. 
  8. $country = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_format_postcode($postcode, $country); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-formatting-functions.php  
  1. function wc_format_postcode( $postcode, $country ) { 
  2. $postcode = wc_normalize_postcode( $postcode ); 
  3.  
  4. switch ( $country ) { 
  5. case 'CA' : 
  6. case 'GB' : 
  7. $postcode = trim( substr_replace( $postcode, ' ', -3, 0 ) ); 
  8. break; 
  9. case 'BR' : 
  10. case 'PL' : 
  11. $postcode = substr_replace( $postcode, '-', -3, 0 ); 
  12. break; 
  13. case 'JP' : 
  14. $postcode = substr_replace( $postcode, '-', 3, 0 ); 
  15. break; 
  16. case 'PT' : 
  17. $postcode = substr_replace( $postcode, '-', 4, 0 ); 
  18. break; 
  19. case 'US' : 
  20. $postcode = rtrim( substr_replace( $postcode, '-', 5, 0 ), '-' ); 
  21. break; 
  22.  
  23. return apply_filters( 'woocommerce_format_postcode', $postcode, $country );