Skip to content

Instantly share code, notes, and snippets.

@hslaszlo
Created June 27, 2017 07:10
Show Gist options
  • Save hslaszlo/447838db3e2e9eb323971a791fae4a57 to your computer and use it in GitHub Desktop.
Save hslaszlo/447838db3e2e9eb323971a791fae4a57 to your computer and use it in GitHub Desktop.
update user meta using select (wordpress)
<?php
// source: https://stackoverflow.com/questions/24960820/wordpress-update-user-meta-using-select/24962153#24962153
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) {
$hub_group = get_user_meta( $user->ID, 'hub_group' );
?>
<h3>Extra profile information</h3>
<table class="form-table">
<tr>
<th><label for="twitter">PfAL Group</label></th>
<td>
<select name="hub_group" id="hub_group">
<option value="PfAL 1" <?php selected( $hub_group[0], "PfAL 1" ); ?>>PfAL 1</option>
<option value="PfAL 2" <?php selected( $hub_group[0], "PfAL 2" ); ?>>PfAL 2</option>
<option value="PfAL 3" <?php selected( $hub_group[0], "PfAL 3" ); ?>>PfAL 3</option>
<option value="PfAL 4" <?php selected( $hub_group[0], "PfAL 4" ); ?>>PfAL 4</option>
</select>
</td>
</tr>
</table>
<?php
}
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'hub_group', $_POST['hub_group'] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment