0

I added a custom user meta field named "Country". Code for adding the custom field:

// 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'] );
}

So, in the backend, it shows "United States (US)". But when I echo it in a shortcode to display them on the frontend for the current user, it shows "united-states".

This is my shortcode to show the current user's country:

    // 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');

How to make it show "United States (US)" instead, on the frontend, in a shortcode?

  • You should include in your question, the full code you use for adding the custom field. – Sally CJ Aug 27 '18 at 5:42
  • @SallyCJ sorry, I've edit and included it now. – Alief Aug 27 '18 at 5:51
  • if my answer helped you, you can accept it and/or upvote it when you have the vote privilege. And if you need further assistance, just let me know. =) – Sally CJ Aug 27 '18 at 8:36
0

(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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.