我添加了一个名为“Country”的自定义用户元字段。添加自定义字段的代码:
// ADD EXTRA PROFILE FIELD
add_action( \'show_user_profile\', \'extrainfo_show_extra_profile_fields\' );
add_action( \'edit_user_profile\', \'extrainfo_show_extra_profile_fields\' );
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 ( 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\',
) 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 }
add_action( \'personal_options_update\', \'extrainfo_save_extra_user_profile_fields\' );
add_action( \'edit_user_profile_update\', \'extrainfo_save_extra_user_profile_fields\' );
function extrainfo_save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( \'edit_user\', $user_id ) )
return false;
update_user_meta( $user_id, \'country\', $_POST[\'country\'] );
}
因此,在后端,它显示“美国(US)”。但是,当我用一个快捷码将其显示在当前用户的前端时,它会显示“美国”。这是显示当前用户所在国家/地区的快捷码:
// USERS\' COUNTRY
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)) {
echo $country;
} else {
echo \'Add your country\';
}
}
}
add_shortcode(\'currentuser_country_info\', \'user_country_info_shortcode\');
如何让它在前端以短代码显示“美国(US)”?