User Custom Field Not Saving
-
I have a function in my theme’s function.php that adds new fields to the user-new registration on the backend.
The text type fields ‘input’ are all getting saved but not the ‘select’ field, here is the select field code:<tr> <th><label for="usersex">Sex</label></th> <td><select name="usersex" id="usersex" class="regular-text"> <option></option> <option value="<?php echo esc_attr( get_the_author_meta( 'usersex', $user->ID ) ); ?>">Male</option> <option value="<?php echo esc_attr( get_the_author_meta( 'usersex', $user->ID ) ); ?>">Female</option> </select> </td> </tr>
Please what could I be getting wrong here.
Thanks.
-
Your option values are the same for either gender, so you couldn’t tell what the user selection is or if it was saved or not. Each option should have a unique value. The place to use the current user meta value is in assigning which option is selected.
Thanks @bcworkz however changing the values of the options, the situation remains the same- not saving.
New code:<tr> <th><label for="usersex">Sex</label></th> <td><select name="usersex" id="usersex" class="regular-text"> <option></option> <option value="<?php echo esc_attr( get_the_author_meta( 'usersex1', $user->ID ) ); ?>">Male</option> <option value="<?php echo esc_attr( get_the_author_meta( 'usersex2', $user->ID ) ); ?>">Female</option> </select> </td> </tr>
What code are you using to save the other custom text input fields?
Thanks @bcworkz Here is whole code I am currently using:
add_action ( 'show_user_profile', 'my_show_extra_profile_fields' ); add_action ( 'edit_user_profile', 'my_show_extra_profile_fields' ); add_action ( 'user_new_form', 'my_show_extra_profile_fields' ); function my_show_extra_profile_fields ( $user ) { ?> <table class="form-table"> <tr> <th><label for="headerlabel"><strong>ADDITIONAL INFORMATION</strong></label></th> </tr> <tr> <th><label for="dateofbirth">Date Of Birth</label></th> <td> <input type="text" name="dateofbirth" id="dateofbirth" value="<?php echo esc_attr( get_the_author_meta( 'dateofbirth', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"></span> </td> </tr> <tr> <th><label for="usersex">Sex</label></th> <td><select name="usersex" id="usersex" class="regular-text"> <option></option> <option value="<?php echo esc_attr( get_the_author_meta( 'usersex1', $user->ID ) ); ?>">Male</option> <option value="<?php echo esc_attr( get_the_author_meta( 'usersex2', $user->ID ) ); ?>">Female</option> </select> </td> </tr> <tr> <th><label for="bloodgroup">Blood Group</label></th> <td> <input type="text" name="bloodgroup" id="bloodgroup" value="<?php echo esc_attr( get_the_author_meta( 'bloodgroup', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"></span> </td> </tr> <tr> <th><label for="genotype">Genotype</label></th> <td> <input type="text" name="genotype" id="genotype" value="<?php echo esc_attr( get_the_author_meta( 'genotype', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"></span> </td> </tr> <tr> <th><label for="homeaddress">Home Address</label></th> <td> <input type="text" name="homeaddress" id="homeaddress" value="<?php echo esc_attr( get_the_author_meta( 'homeaddress', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"></span> </td> </tr> <tr> <th><label for="stateoforigin">State Of Origin</label></th> <td> <input type="text" name="stateoforigin" id="stateoforigin" value="<?php echo esc_attr( get_the_author_meta( 'stateoforigin', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"></span> </td> </tr> <tr> <th><label for="fatherphone">Father's Phone Number</label></th> <td> <input type="text" name="fatherphone" id="fatherphone" value="<?php echo esc_attr( get_the_author_meta( 'fatherphone', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"></span> </td> </tr> <tr> <th><label for="motherphone">Mother's Phone Number</label></th> <td> <input type="text" name="motherphone" id="motherphone" value="<?php echo esc_attr( get_the_author_meta( 'motherphone', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"></span> </td> </tr> <tr> <th><label for="previousschools">Previous Schools Attended</label></th> <td> <input type="text" name="previousschools" id="previousschools" value="<?php echo esc_attr( get_the_author_meta( 'previousschools', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"></span> </td> </tr> <!--tr> <th><label for="additionalnotes">Additional Notes</label></th> <td> <textarea type="text" name="additionalnotes" id="additionalnotes" value="<?php echo esc_attr( get_the_author_meta( 'additionalnotes', $user->ID ) ); ?>" class="regular-text" /></textarea> <span class="description"></span> </td> </tr--> </table> <?php } add_action ( 'personal_options_update', 'my_save_extra_profile_fields' ); add_action ( 'edit_user_profile_update', 'my_save_extra_profile_fields' ); add_action ( 'user_new_form', 'my_save_extra_profile_fields' ); //save them function my_save_extra_profile_fields( $user_id ) { if ( !current_user_can( 'edit_user', $user_id ) ) return false; update_usermeta( $user_id, 'dateofbirth', $_POST['dateofbirth'] ); update_usermeta( $user_id, 'usersex1', $_POST['usersex1'] ); update_usermeta( $user_id, 'usersex2', $_POST['usersex2'] ); update_usermeta( $user_id, 'homeaddress', $_POST['homeaddress'] ); update_usermeta( $user_id, 'stateoforigin', $_POST['stateoforigin'] ); update_usermeta( $user_id, 'fatherphone', $_POST['fatherphone'] ); update_usermeta( $user_id, 'motherphone', $_POST['motherphone'] ); update_usermeta( $user_id, 'bloodgroup', $_POST['bloodgroup'] ); update_usermeta( $user_id, 'genotype', $_POST['genotype'] ); update_usermeta( $user_id, 'previousschools', $_POST['previousschools'] ); update_usermeta( $user_id, 'additionalnotes', $_POST['additionalnotes'] ); } add_action('user_register', 'my_save_extra_profile_fields'); the_author_meta( $meta_key, $user_id );
You need to get the selected value from
$_POST['usersex']
based on the name of the select form field. It will contain the selected option value. Thus you don’t really need two fields to save one selected value. It can be made to work with two fields, but there’s little point. I realize you added this at my instigation, I wasn’t clear what I was after then.When you output the option values, that can be hard coded “male” or “female” or whatever. When you get the currently stored value, use that to determine which option is preselected. As a convenience, you can use selected() to determine which option gets that attribute. Examples in the linked article.
Perhaps you’re aware, but just in case, you will eventually need to validate and sanitize all user input fields before saving such values to your DB. It’s OK to temporarily forgo this while developing in order to not complicate things, but please do add in these important security measures before you’re finished.
- The topic ‘User Custom Field Not Saving’ is closed to new replies.