I am making custom fields for users in admin panel like this:
function additional_profile_fields( $user ) {
$departments = get_terms(['taxonomy' => 'department', 'hide_empty' => false]);
$userDepartment = get_the_author_meta( 'department', $user->ID );
$regions = get_terms(['taxonomy' => 'region', 'hide_empty' => false]);
$userRegion = get_the_author_meta( 'region', $user->ID );
$industries = get_terms(['taxonomy' => 'industry', 'hide_empty' => false]);
$userIndustry = get_the_author_meta( 'industry', $user->ID );
$companies = get_terms(['taxonomy' => 'company', 'hide_empty' => false]);
$userCompany = get_the_author_meta( 'company', $user->ID );
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="department">Avdeling</label></th>
<td>
<select id="department" name="department"><?php
foreach ( $departments as $department ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $department->name, selected( $userDepartment, $department->name, false ) );
}
?></select>
</td>
</tr>
</table>
<table class="form-table">
<tr>
<th><label for="region">Region</label></th>
<td>
<select id="region" name="region"><?php
foreach ( $regions as $region ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $region->name, selected( $userRegion, $region->name, false ) );
}
?></select>
</td>
</tr>
</table>
<table class="form-table">
<tr>
<th><label for="industry">Bransje</label></th>
<td>
<select id="industry" name="industry"><?php
foreach ( $industries as $industry ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $industry->name, selected( $userIndustry, $industry->name, false ) );
}
?></select>
</td>
</tr>
</table>
<table class="form-table">
<tr>
<th><label for="company">Selskap</label></th>
<td>
<select id="company" name="company"><?php
foreach ( $companies as $company ) {
printf( '<option value="%1$s" %2$s>%1$s</option>', $company->name, selected( $userCompany, $company->name, false ) );
}
?></select>
</td>
</tr>
</table>
<?php
}
add_action('user_new_form', 'additional_profile_fields');
add_action('edit_user_profile', 'tm_additional_profile_fields');
add_action('show_user_profile', 'tm_additional_profile_fields');
This works fine, but this gives me always as a default first option from any array that I am looping through, but I would like to present a default option field like placeholder if the user doesn't have anything in the DB for that field. How can I do that?