Search code examples
phpdatabasewordpressuser-profile

Adding Country and State Selection to User Profile Wordpress


Hello I am trying to create a drop down selection for Country and State on our website, I have the state working correctly but the country seems to be saving but not displaying. When a user selects state, the state displays but when a user selects country the country saves but doesn't display. What am I missing?

//add profile field location
add_action( 'show_user_profile', 'country_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'country_show_extra_profile_fields' );

function country_show_extra_profile_fields( $user ) { ?>
    <h3>Geographic Information</h3>
    <table class="form-table">
        <tr>
            <th><label for="country">Country</label></th>
            <td>
                <select name="country" id="country" >
                    <option value="not-selected" <?php selected( 'not-selected', get_the_author_meta( 'country', $user->ID ) ); ?>> </option>
                    <option value="unitedstates" <?php selected( 'united-states', get_the_author_meta( 'country', $user->ID ) ); ?>>United States</option>
                    <option value="uk" <?php selected( 'united-kingdom', get_the_author_meta( 'country', $user->ID ) ); ?>>United Kingdom</option>
                    <option value="australia" <?php selected( 'Australia', get_the_author_meta( 'country', $user->ID ) ); ?>>Australia</option>
                    <option value="africa" <?php selected( 'Africa', get_the_author_meta( 'country', $user->ID ) ); ?>>Africa</option>
                    <option value="canada" <?php selected( 'Canada', get_the_author_meta( 'country', $user->ID ) ); ?>>Canada</option>
                    <option value="china" <?php selected( 'China', get_the_author_meta( 'country', $user->ID ) ); ?>>China</option>
                    <option value="germany" <?php selected( 'Germany', get_the_author_meta( 'country', $user->ID ) ); ?>>Germany</option>
                    <option value="skorea" <?php selected( 'South Korea', get_the_author_meta( 'country', $user->ID ) ); ?>>South Korea</option>
                    <option value="russia" <?php selected( 'Russia', get_the_author_meta( 'country', $user->ID ) ); ?>>Russia</option>
                    <option value="israel" <?php selected( 'Israel', get_the_author_meta( 'country', $user->ID ) ); ?>>Israel</option>
                    <option value="mexico" <?php selected( 'Mexico', get_the_author_meta( 'country', $user->ID ) ); ?>>Mexico</option>
                </select>
            </td>
        </tr>
    </table>
<?php }

add_action( 'personal_options_update', 'country_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'country_save_extra_user_profile_fields' );

function country_save_extra_user_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    update_usermeta( $user_id, 'country', $_POST['country'] );
}
//State
add_action( 'show_user_profile', 'state_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'state_show_extra_profile_fields' );

function state_show_extra_profile_fields( $user ) { ?>
    <table class="form-table">
        <tr>
            <th><label for="state">US State</label></th>
            <td>
                <select name="state" id="state" >
                    <option value="not-selected" <?php selected( 'not-selected', get_the_author_meta( 'state', $user->ID ) ); ?>> </option>
                    <option value="alabama" <?php selected( 'alabama', get_the_author_meta( 'state', $user->ID ) ); ?>>Alabama</option>
                    <option value="alaska" <?php selected( 'alaska', get_the_author_meta( 'state', $user->ID ) ); ?>>Alaska</option>
                    <option value="arizona" <?php selected( 'arizona', get_the_author_meta( 'state', $user->ID ) ); ?>>Arizona</option>
                    <option value="arkansas" <?php selected( 'arkansas', get_the_author_meta( 'state', $user->ID ) ); ?>>Arkansas</option>
                    <option value="california" <?php selected( 'california', get_the_author_meta( 'state', $user->ID ) ); ?>>California</option>
                    <option value="colorado" <?php selected( 'colorado', get_the_author_meta( 'state', $user->ID ) ); ?>>Colorado</option>
                    <option value="connecticut" <?php selected( 'connecticut', get_the_author_meta( 'state', $user->ID ) ); ?>>Connecticut</option>
                    <option value="delaware" <?php selected( 'delaware', get_the_author_meta( 'state', $user->ID ) ); ?>>Delaware</option>
                    <option value="florida" <?php selected( 'florida', get_the_author_meta( 'state', $user->ID ) ); ?>>Florida</option>
                    <option value="georgia" <?php selected( 'georgia', get_the_author_meta( 'state', $user->ID ) ); ?>>Georgia</option>
                    <option value="hawaii" <?php selected( 'hawaii', get_the_author_meta( 'state', $user->ID ) ); ?>>Hawaii</option>
                    <option value="idaho" <?php selected( 'idaho', get_the_author_meta( 'state', $user->ID ) ); ?>>Idaho</option>
                    <option value="illinois" <?php selected( 'illinois', get_the_author_meta( 'state', $user->ID ) ); ?>>Illinois</option>
                    <option value="indiana" <?php selected( 'indiana', get_the_author_meta( 'state', $user->ID ) ); ?>>Indiana</option>
                    <option value="iowa" <?php selected( 'iowa', get_the_author_meta( 'state', $user->ID ) ); ?>>Iowa</option>
                    <option value="kansas" <?php selected( 'kansas', get_the_author_meta( 'state', $user->ID ) ); ?>>Kansas</option>
                    <option value="kentucky" <?php selected( 'kentucky', get_the_author_meta( 'state', $user->ID ) ); ?>>Kentucky</option>
                    <option value="louisiana" <?php selected( 'louisiana', get_the_author_meta( 'state', $user->ID ) ); ?>>Louisiana</option>
                    <option value="maine" <?php selected( 'maine', get_the_author_meta( 'state', $user->ID ) ); ?>>Maine</option>
                    <option value="maryland" <?php selected( 'maryland', get_the_author_meta( 'state', $user->ID ) ); ?>>Maryland</option>
                    <option value="massachusetts" <?php selected( 'massachusetts', get_the_author_meta( 'state', $user->ID ) ); ?>>Massachusetts</option>
                    <option value="michigan" <?php selected( 'michigan', get_the_author_meta( 'state', $user->ID ) ); ?>>Michigan</option>
                    <option value="minnesota" <?php selected( 'minnesota', get_the_author_meta( 'state', $user->ID ) ); ?>>Minnesota</option>
                    <option value="mississippi" <?php selected( 'mississippi', get_the_author_meta( 'state', $user->ID ) ); ?>>Mississippi</option>
                    <option value="missouri" <?php selected( 'missouri', get_the_author_meta( 'state', $user->ID ) ); ?>>Missouri</option>
                    <option value="montana" <?php selected( 'montana', get_the_author_meta( 'state', $user->ID ) ); ?>>Montana</option>
                    <option value="nebraska" <?php selected( 'nebraska', get_the_author_meta( 'state', $user->ID ) ); ?>>Nebraska</option>
                    <option value="nevada" <?php selected( 'nevada', get_the_author_meta( 'state', $user->ID ) ); ?>>Nevada</option>
                    <option value="new-hampshire" <?php selected( 'new-hampshire', get_the_author_meta( 'state', $user->ID ) ); ?>>New Hampshire</option>
                    <option value="new-jersey" <?php selected( 'new-jersey', get_the_author_meta( 'state', $user->ID ) ); ?>>New Jersey</option>
                    <option value="new-mexico" <?php selected( 'new-mexico', get_the_author_meta( 'state', $user->ID ) ); ?>>New Mexico</option>
                    <option value="new-york" <?php selected( 'new-york', get_the_author_meta( 'state', $user->ID ) ); ?>>New York</option>
                    <option value="north-carolina" <?php selected( 'north-carolina', get_the_author_meta( 'state', $user->ID ) ); ?>>North Carolina</option>
                    <option value="north-dakota" <?php selected( 'north-dakota', get_the_author_meta( 'state', $user->ID ) ); ?>>North Dakota</option>
                    <option value="ohio" <?php selected( 'ohio', get_the_author_meta( 'state', $user->ID ) ); ?>>Ohio</option>
                    <option value="oklahoma" <?php selected( 'oklahoma', get_the_author_meta( 'state', $user->ID ) ); ?>>Oklahoma</option>
                    <option value="oregon" <?php selected( 'oregon', get_the_author_meta( 'state', $user->ID ) ); ?>>Oregon</option>
                    <option value="pennsylvania" <?php selected( 'pennsylvania', get_the_author_meta( 'state', $user->ID ) ); ?>>Pennsylvania</option>
                    <option value="rhode-island" <?php selected( 'rhode-island', get_the_author_meta( 'state', $user->ID ) ); ?>>Rhode Island</option>
                    <option value="south-carolina" <?php selected( 'south-carolina', get_the_author_meta( 'state', $user->ID ) ); ?>>South Carolina</option>
                    <option value="south-dakota" <?php selected( 'south-dakota', get_the_author_meta( 'state', $user->ID ) ); ?>>South Dakota</option>
                    <option value="tennessee" <?php selected( 'tennessee', get_the_author_meta( 'state', $user->ID ) ); ?>>Tennessee</option>
                    <option value="texas" <?php selected( 'texas', get_the_author_meta( 'state', $user->ID ) ); ?>>Texas</option>
                    <option value="utah" <?php selected( 'utah', get_the_author_meta( 'state', $user->ID ) ); ?>>Utah</option>
                    <option value="vermont" <?php selected( 'vermont', get_the_author_meta( 'state', $user->ID ) ); ?>>Vermont</option>
                    <option value="virginia" <?php selected( 'virginia', get_the_author_meta( 'state', $user->ID ) ); ?>>Virginia</option>
                    <option value="washington" <?php selected( 'washington', get_the_author_meta( 'state', $user->ID ) ); ?>>Washington</option>
                    <option value="west-virginia" <?php selected( 'west-virginia', get_the_author_meta( 'state', $user->ID ) ); ?>>West Virginia</option>
                    <option value="wisconsin" <?php selected( 'wisconsin', get_the_author_meta( 'state', $user->ID ) ); ?>>Wisconsin</option>
                    <option value="wyoming" <?php selected( 'wyoming', get_the_author_meta( 'state', $user->ID ) ); ?>>Wyoming</option>
                </select>
            </td>
        </tr>
    </table>
<?php }

add_action( 'personal_options_update', 'state_save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'state_save_extra_user_profile_fields' );

function state_save_extra_user_profile_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;   

    update_usermeta( $user_id, 'state', $_POST['state'] );
}

Solution

  • The Answer

    Use get_user_meta() and not get_the_author_meta().

    Other Notes

    update_usermeta() has been deprecated. Therefore,

    Use update_user_meta() and not update_usermeta().

    And if I may suggest a shorter (i.e. smaller in size) version of your code:

    <?php
    //add profile field location
    add_action( 'show_user_profile', 'country_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'country_show_extra_profile_fields' );
    
    function country_show_extra_profile_fields( $user ) { ?>
        <h3>Geographic Information</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' => '',
                                'unitedstates' => 'United States',
                                'uk' => 'United Kingdom',
                                'australia' => 'Australia',
                                'africa' => 'Africa',
                                'canada' => 'Canada',
                                'china' => 'China',
                                'germany' => 'Germany',
                                'skorea' => 'South Korea',
                                'russia' => 'Russia',
                                'israel' => 'Israel',
                                'mexico' => 'Mexico',
                            ) 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', 'country_save_extra_user_profile_fields' );
    add_action( 'edit_user_profile_update', 'country_save_extra_user_profile_fields' );
    
    function country_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'] );
    }
    //State
    add_action( 'show_user_profile', 'state_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'state_show_extra_profile_fields' );
    
    function state_show_extra_profile_fields( $user ) { ?>
        <table class="form-table">
            <tr>
                <th><label for="state">US State</label></th>
                <td>
                    <select name="state" id="state" >
                        <?php
                            $_value = trim( get_user_meta( $user->ID, 'state', true ) );
                            foreach ( array(
                                'not-selected' => '',
                                'alabama' => 'Alabama',
                                'alaska' => 'Alaska',
                                'arizona' => 'Arizona',
                                'arkansas' => 'Arkansas',
                                'california' => 'California',
                                'colorado' => 'Colorado',
                                'connecticut' => 'Connecticut',
                                'delaware' => 'Delaware',
                                'florida' => 'Florida',
                                'georgia' => 'Georgia',
                                'hawaii' => 'Hawaii',
                                'idaho' => 'Idaho',
                                'illinois' => 'Illinois',
                                'indiana' => 'Indiana',
                                'iowa' => 'Iowa',
                                'kansas' => 'Kansas',
                                'kentucky' => 'Kentucky',
                                'louisiana' => 'Louisiana',
                                'maine' => 'Maine',
                                'maryland' => 'Maryland',
                                'massachusetts' => 'Massachusetts',
                                'michigan' => 'Michigan',
                                'minnesota' => 'Minnesota',
                                'mississippi' => 'Mississippi',
                                'missouri' => 'Missouri',
                                'montana' => 'Montana',
                                'nebraska' => 'Nebraska',
                                'nevada' => 'Nevada',
                                'new-hampshire' => 'New Hampshire',
                                'new-jersey' => 'New Jersey',
                                'new-mexico' => 'New Mexico',
                                'new-york' => 'New York',
                                'north-carolina' => 'North Carolina',
                                'north-dakota' => 'North Dakota',
                                'ohio' => 'Ohio',
                                'oklahoma' => 'Oklahoma',
                                'oregon' => 'Oregon',
                                'pennsylvania' => 'Pennsylvania',
                                'rhode-island' => 'Rhode Island',
                                'south-carolina' => 'South Carolina',
                                'south-dakota' => 'South Dakota',
                                'tennessee' => 'Tennessee',
                                'texas' => 'Texas',
                                'utah' => 'Utah',
                                'vermont' => 'Vermont',
                                'virginia' => 'Virginia',
                                'washington' => 'Washington',
                                'west-virginia' => 'West Virginia',
                                'wisconsin' => 'Wisconsin',
                                'wyoming' => 'Wyoming',
                            ) 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', 'state_save_extra_user_profile_fields' );
    add_action( 'edit_user_profile_update', 'state_save_extra_user_profile_fields' );
    
    function state_save_extra_user_profile_fields( $user_id ) {
    
        if ( !current_user_can( 'edit_user', $user_id ) )
            return false;   
    
        update_user_meta( $user_id, 'state', $_POST['state'] );
    }