2

I've added this code snippet to my functions.php

function my_new_contactmethods( $contactmethods ) {  
    $contactmethods['country'] = 'country';
    return $contactmethods;
}
add_filter('user_contactmethods','my_new_contactmethods',10,1);

It works perfactly as I wanted. It show an extra field to fill up user country on user backend profile.

But it just a text field. Actually I want to show this field as a drop down menu which has all the countries.

How can I modify this code to get a country list dropdown menu ?

1 Answer 1

2

Try this

/* Save selected data */
add_action( 'personal_options_update', 'save_user_fields' );
add_action( 'edit_user_profile_update', 'save_user_fields' );

function save_user_fields( $user_id ) {

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

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

add_action( 'show_user_profile', 'Add_user_fields' );
add_action( 'edit_user_profile', 'Add_user_fields' );

function Add_user_fields( $user ) {

?>

<h3>Additional Field</h3>
<table class="form-table">       

    <tr>
        <th><label for="dropdown">Select field</label></th>
        <td>
            <?php 
            //get dropdown saved value
            $selected = get_the_author_meta( 'country', $user->ID ); 
            ?>
            <select name="country" id="country">
                <option value="Argentina" <?php echo ($selected == "Argentina")?  'selected="selected"' : ''; ?>>Argentina</option>
                <option value="Belgium" <?php echo ($selected == "Belgium")?  'selected="selected"' : ''; ?>>Belgium</option>
                <option value="countryX" <?php echo ($selected == "countryX")?  'selected="selected"' : ''; ?>>country X</option>
            </select>
            <span class="description">Select the above</span>
        </td>
    </tr>
</table>
<?php 
}
?>

Hope this does your job.

4
  • Thanks for the code snippet Shreyo. Now I see the country dropdown list at the user profile. but it is not saved when I change the country. Eg: from the above list if I select Belgium and press the button Update Profile then again I see Argentina as default. Seems it is not get saved when we click Update Profile button. do we have to something else to get it saved? Commented Jan 27, 2015 at 6:29
  • I will check if i missed any code. It's a working code.
    – Shreyo Gi
    Commented Jan 27, 2015 at 6:39
  • @RiffazStarr, update this section: update_usermeta( $user_id, 'country', $_POST['country'] ); updated the code too.
    – Shreyo Gi
    Commented Jan 27, 2015 at 6:46
  • Thanks a lot for the update.. works like a charm.. How can I move this drop down list to contact info section where wordpress already has email and website?? Commented Jan 27, 2015 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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