Ahh it turns out it wasn’t able to stay selected because I was using the “strtolower” function to remove capital letters. Which in turn caused there to be a mismatch between the selected value and the saved value in the database.
add_action( 'show_user_profile', 'product_selection_field', 3 );
add_action( 'edit_user_profile', 'product_selection_field', 3 );
function product_selection_field( $user ) {
if( current_user_can('free') || current_user_can('administrator') ) {
$user_ID = get_current_user_id();
$current_product = get_user_meta( $user_ID, "club_choice", true );
$args = array( 'post_type' => 'product', 'product_tag' => 'partner' );
$products = get_posts( $args );
foreach ( $products as $page ) {
$string = $page->post_title;
$product_option .= "<option value="" . $page->post_title . "" " . selected( $current_product, $string, false ) . ">";
$product_option .= $page->post_title;
$product_option .= "</option>";
}
?>
<div class="form-group full-width">
<?php
$user_ID = get_current_user_id();
$all_meta_for_user = get_user_meta( $user_ID, "club_choice", true );
echo '<label><strong>Your current club selected is: </label>';
print_r( $all_meta_for_user );
echo '</strong><br/>';
?>
</div>
<div class="form-group full-width">
<label for="club_choice"><?php _e("Select your club"); ?></label>
<select name="club_choice" id="club_choice" class="input">
<?php
echo $product_option;
?>
</select>
</div>
<?php
}
}
// then I save the field
add_action( 'personal_options_update', 'adding_club_reg_fields', 10, 1 );
add_action( 'user_register', 'adding_club_reg_fields', 10, 1 );
function adding_club_reg_fields( $user_id ) {
if ( isset( $_POST['club_choice'] ) ) {
update_user_meta( $user_id, 'club_choice', $_POST['club_choice'] );
}
}