(Updated/new answer)
How to make it show “United States (US)” instead, on the frontend, in
a shortcode?
First off, you got united-states
(the country slug/key) instead of United States (US)
(the country name) because your extrainfo_save_extra_user_profile_fields()
function is saving the country slug/key instead of the country name. That’s not a problem, and I’d do the same thing.
However, to display the proper country name based on its slug/key, then:
An easy way, is by copying the country slugs/keys and names array
from your extrainfo_show_extra_profile_fields()
function to your shortcode function; e.g.:
function user_country_info_shortcode() {
if(is_user_logged_in()) {
$user_id = get_current_user_id();
$country = get_user_meta($user_id, 'country', true);
if (!empty($country)) {
$countrys = array(
'not-selected' => '',
'south-africa' => 'South Africa',
'south-korea' => 'South Korea',
'spain' => 'Spain',
'ukraine' => 'United Arab Emirates',
'united-kingdom' => 'United Kingdom (UK)',
'united-states' => 'United States (US)',
'venezuela' => 'Venezuela',
'vietnam' => 'Vietnam',
);
$country = isset( $countrys[ $country ] ) ? $countrys[ $country ] : $country;
echo $country;
} else {
echo 'Add your country';
}
}
}
But for more flexibility — no need to edit both the functions code just to change a specific country slug/key and/or name, then I strongly suggest you to go with the other option below, particularly if and when you have a (very) long list of countries:
In the theme functions file (e.g. wp-content/themes/your-child-theme/functions.php
), or in your plugin file which stores global functions, add this:
// List of country slugs and names.
function my_user_country_list() {
return array(
'not-selected' => '',
'south-africa' => 'South Africa',
'south-korea' => 'South Korea',
'spain' => 'Spain',
'ukraine' => 'United Arab Emirates',
'united-kingdom' => 'United Kingdom (UK)',
'united-states' => 'United States (US)',
'venezuela' => 'Venezuela',
'vietnam' => 'Vietnam',
);
}
Then in the extrainfo_show_extra_profile_fields()
function, replace the foreach
(opening line) so that it utilizes the my_user_country_list()
function, like this:
function extrainfo_show_extra_profile_fields( $user ) { ?>
<h3 class="extra-info">Extra Info</h3>
<table class="form-table">
<tr>
<th><label for="country">Country</label></th>
<td>
<select name="country" id="country" >
<?php
$_value = trim( get_user_meta( $user->ID, 'country', true ) );
foreach ( my_user_country_list() as $value => $label ) :
$selected = selected( $value, $_value, false );
?>
<option value="<?php echo esc_attr( $value ); ?>"<?php echo $selected; ?>><?php echo esc_html( $label ); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
</table>
<?php }
And then, in your user_country_info_shortcode()
function, you can retrieve the user’s country (i.e. the country name) like so: (re-indented for clarity)
function user_country_info_shortcode() {
if(is_user_logged_in()) {
$user_id = get_current_user_id();
$country = get_user_meta($user_id, 'country', true);
if (!empty($country)) {
$countrys = my_user_country_list();
$country = isset( $countrys[ $country ] ) ? $countrys[ $country ] : $country;
echo $country;
} else {
echo 'Add your country';
}
}
}
Sorry for the many edits, but hopefully this “new” answer would be of more help than the original answer (or the previous revisions of this answer).