user2701647
user2701647

Reputation:

wordpress update user meta using select

Trying to add my own custom fields to user profiles.

When using update_user_meta I can't seem to get the data out of $_POST correctly. However if I hard code the value with a string, it works just fine. I know the value inside of $_POST['hub_group'] may be an array, i've tried converting to a string or just referencing the first index but nothing has worked.

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 ) {
        if (isset($_POST['hub_group'])) {
            update_user_meta($user->ID, 'hub_group', $_POST['hub_group']);
        }       
        $hub_group = get_the_author_meta( 'hub_group', $user->ID);
    ?>
    <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, "PfAL 1" ); ?>>PfAL 1</option>
                  <option value="PfAL 2" <?php selected( $hub_group, "PfAL 2" ); ?>>PfAL 2</option>
                  <option value="PfAL 3" <?php selected( $hub_group, "PfAL 3" ); ?>>PfAL 3</option>
                  <option value="PfAL 4" <?php selected( $hub_group, "PfAL 4" ); ?>>PfAL 4</option>
                </select>
            </td>
        </tr>
    </table>
<?php }

Upvotes: 0

Views: 2317

Answers (1)

jjjjjjjjjjjjjjjjjjjj
jjjjjjjjjjjjjjjjjjjj

Reputation: 3118

I managed to solve it by dividing the update and display logic, then hooking the update logic function to personal_options_update & edit_user_profile_update. Code below is working for me, I hope it does the same for you!

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'] );

}

Upvotes: 1

Related Questions