There are 3 main issues in your code:
-
I see you’re using
current_user_id()
which does not exist in WordPress, so I believe that should beget_current_user_id()
. -
Your
update_basic_user_meta()
function basically would work in updating the user’s metadata, but you need to check whether the POST data (gender
andspecialty
) are actually set before proceeding to update the metadata, and that the GET data namedupdate_basic_user_meta
is also set, which means the form was submitted.However, if I were you, I would use a nonce field than a simple GET query.
-
You need to highlight the current selection for the “Gender” and “Specialty” options, so that users know what have they selected and whether it was actually saved. So,
-
For the “Gender” option (which uses
select
menu), you can useselected()
. -
For the “Specialty” option (which uses
radio
buttons), you can usechecked()
.
-
And despite add_filter()
works, init
is an action hook, so you should use add_action()
:
add_action( 'init', 'update_basic_user_meta' ); // use this
//add_filter( 'init', 'update_basic_user_meta' ); // not this
Sample snippets that would make your form works as expected
-
Highlight the current selection for the “Gender” option:
<?php $gender = get_user_meta( get_current_user_id(), 'gender', true ); ?> <select name="gender"> <option value="male"<?php selected( 'male', $gender ); ?>><?php _e( 'Male', 'text-domain' ); ?></option> <option value="female"<?php selected( 'female', $gender ); ?>><?php _e( 'Female', 'text-domain' ); ?></option> </select>
-
Highlight the current selection for the “Specialty” option:
<?php $specialty = get_user_meta( get_current_user_id(), 'specialty', true ); ?> <div> <label><input type="radio" name="specialty" value="0"<?php checked( '0', $specialty ); ?>> <?php _e( 'Read', 'text-domain' ); ?></label><br> <label><input type="radio" name="specialty" value="1"<?php checked( '1', $specialty ); ?>> <?php _e( 'Write', 'text-domain' ); ?></label><br> <label><input type="radio" name="specialty" value="2"<?php checked( '2', $specialty ); ?>> <?php _e( 'Translate', 'text-domain' ); ?></label> </div>
-
Function to update the user’s metadata:
function update_basic_user_meta() { if ( ! empty( $_GET['update_basic_user_meta'] ) && isset( $_POST['gender'], $_POST['specialty'] ) && $user_id = get_current_user_id() ) { update_user_meta( $user_id, 'gender', sanitize_text_field( $_POST['gender'] ) ); update_user_meta( $user_id, 'specialty', sanitize_text_field( $_POST['specialty'] ) ); if ( ! empty( $_POST['redirect_to'] ) ) { wp_redirect( $_POST['redirect_to'] ); exit; } } }