Created
August 18, 2016 21:25
-
-
Save jaredatch/4de14260dbfdc1d5758398071ad00660 to your computer and use it in GitHub Desktop.
WPForms Address field store full country name instead of country code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Use full country name instead of the country code. | |
| * | |
| * @param array $fields | |
| * @param array $entry | |
| * @param array $form_data | |
| * @return array | |
| */ | |
| function wpf_address_full_country( $fields, $entry, $form_data ) { | |
| // Get the array of all available countries. Array key is country code, | |
| // value is country name. | |
| $countries = wpforms_countries(); | |
| // Loop through all the submitted fields | |
| foreach( $fields as $field_id => $field ) { | |
| // Look for address fields that have a country provided | |
| if ( !empty( $field['type'] ) && 'address' == $field['type'] && !empty( $field['country'] ) ) { | |
| // Presumably, the country code | |
| $country_code = $field['country']; | |
| // Make sure everything checks out | |
| if ( !empty( $countries[$country_code] ) ) { | |
| // Use country code to get full country name | |
| $country = $countries[$country_code]; | |
| // Replace country code with full country | |
| $fields[$field_id]['country'] = $country; | |
| $fields[$field_id]['value'] = str_replace( $country_code, $country, $fields[$field_id]['value'] ); | |
| } | |
| } | |
| } | |
| return $fields; | |
| } | |
| add_filter( 'wpforms_process_filter', 'wpf_address_full_country', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment