L
L
ligisayan2016-03-30 10:24:28
PHP
ligisayan, 2016-03-30 10:24:28

How to add radio button field to user profile?

There is a site on wordpress, I want to add the ability to select gender (male / female) to the user profile using the radiobutton radiobutton. I prescribe a filter in the functions.php file

add_filter('user_contactmethods', 'my_user_gender');

function my_user_gender($user_contactmethods){
  $user_contactmethods['gender'] = 'Пол';
  return $user_contactmethods;
}

There is a site on wordpress, I want to add the ability to select gender (male / female) to the user profile using the radiobutton switcher. I write in the functions.php file the filter
add_filter('user_contactmethods', 'my_user_gender');
function my_user_gender($user_contactmethods){
$user_contactmethods['gender'] = 'Gender';
return $user_contactmethods;
}
However, in the profile I get a standard input field opposite the inscription "Gender". How can I make this field look like a radio button?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
ligisayan, 2016-03-30
@ligisayan

// дополнительные данные на странице профиля
add_action('show_user_profile', 'my_profile_new_fields_add');
add_action('edit_user_profile', 'my_profile_new_fields_add');

add_action('personal_options_update', 'my_profile_new_fields_update');
add_action('edit_user_profile_update', 'my_profile_new_fields_update');

function my_profile_new_fields_add($user){ 
?>
  <h3>Дополнительные данные</h3>
  <table class="form-table">
        <tr>
      <th><label for="gender">Пол</label></th>
      <td>
                <p><input type="radio" name="gender" value="мужской" <?php checked('мужской', get_user_meta($user->ID, 'gender', true)); ?>> мужской</p>
                <p><input type="radio" name="gender" value="женский" <?php checked('женский', get_user_meta($user->ID, 'gender', true)); ?>> женский</p>
      </td>
    </tr>
  </table>
  <?php            
}

// обновление
function my_profile_new_fields_update($user_id){
  if ( !current_user_can( 'edit_user', $user_id ) )
    return false;
  update_usermeta( $user_id, 'gender', $_POST['gender'] );
}

P
Pshkll, 2016-03-30
@Pshkll

<?php
    add_action( 'show_user_profile', 'show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'show_extra_profile_fields' );

    function show_extra_profile_fields( $user ) { ?>
        <h3>Extra profile information</h3>
        <table class="form-table">
            <tr>
                <th><label for="gender">Пол</label></th>
                <td>
                    <select name="gender" id="gender" >
                        <option value="Male" <?php selected( 'Male', get_the_author_meta( 'gender', $user->ID ) ); ?>>Мужчина</option>
                        <option value="Female" <?php selected( 'Female', get_the_author_meta( 'gender', $user->ID ) ); ?>>Женщина</option>
                    </select>
                </td>
            </tr>
        </table>
    <?php }

    add_action( 'personal_options_update', 'save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'save_extra_profile_fields' );

    function save_extra_profile_fields( $user_id ) {
        if ( !current_user_can( 'edit_user', $user_id ) )
            return false;
        update_usermeta( $user_id, 'gender', $_POST['gender'] );
    }
?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question