Home / Archive / MemberPress: Replace Country Code with Country Name
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Replace Country Code with Country Name

Replaces the 2-digit country code in the {$user_address} parameter for emails with the full country name.

Code Preview
php
<?php
function mepr_replace_country_code_with_country_name ( $address, $user ) {
    $countries = require( MEPR_I18N_PATH . '/countries.php' );
    $addr1     = get_user_meta( $user->ID, 'mepr-address-one',     true );
    $addr2     = get_user_meta( $user->ID, 'mepr-address-two',     true );
    $city      = get_user_meta( $user->ID, 'mepr-address-city',    true );
    $state     = get_user_meta( $user->ID, 'mepr-address-state',   true );
    $zip       = get_user_meta( $user->ID, 'mepr-address-zip',     true );
    $country   = get_user_meta( $user->ID, 'mepr-address-country', true );
      
      if( empty( $addr1 ) or empty( $city ) || empty( $state ) or empty( $zip ) ) {
      return $address;
    }
  
    $addr = $addr1;
  
    if( $addr2 and !empty( $addr2 ) ) { $addr .= "<br/>{$addr2}"; }
    if( $country and !empty( $country )) { $country = "<br/>{$countries[$country]}"; } else { $country = ''; }
  
    $addr = sprintf( __( '<br/>%1$s<br/>%2$s, %3$s %4$s%5$s<br/>', 'memberpress' ), $addr, $city, $state, $zip, $country );
      
      return $addr;
  };
  
add_filter( 'mepr-user-formatted-address', 'mepr_replace_country_code_with_country_name', 2, 10);

Comments

Add a Comment