Answer the question
In order to leave comments, you need to log in
How to output (remember) the selected option for a select tag?
Good afternoon!
I need your help :) I'm making my own theme for wordpress, I need to be able to set custom fields in the user profile. I figured out the input tag, but decided to refine it a little so that I could select a value from the drop-down list, in general everything works, but after restarting the page, the first value is displayed, and not what I chose. At the same time, the passed value is displayed on the pages where I display this value (as it should be).
Here is a piece of code:
/* добавление поля в профиле история заказов*/
add_action( 'show_user_profile', 'add_extra_social_links4' );
add_action( 'edit_user_profile', 'add_extra_social_links4' );
function add_extra_social_links4( $user )
{
?>
Склад назначения:<br/>
<select placeholder="Склад назначения" name="sklad-zakaz1" value="<?php echo esc_attr(get_the_author_meta( 'sklad-zakaz1', $user->ID )); ?>" class="regular-text" /><br/>
<option value="1">1</option>
<option value="2">1</option>
<option value="3">3</option>
</select>
// сохранение
add_action( 'personal_options_update', 'save_extra_social_links4' );
add_action( 'edit_user_profile_update', 'save_extra_social_links4' );
function save_extra_social_links4( $user_id )
{
update_user_meta( $user_id,'sklad-zakaz2', sanitize_text_field( $_POST['sklad-zakaz1'] ) );
}
<?php echo get_user_meta($user_ID, 'sklad-zakaz1', true ) ?>
Answer the question
In order to leave comments, you need to log in
select tag has no value attribute
<option
value="1"
<?php if (get_user_meta($user_ID, 'sklad-zakaz1', true ) == "1") ?>selected<?php endif ?>
>...</option>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question