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