1
votes

I'm trying to add and change the input type from text to radio button to use in my wordpress theme user profile settings like Gender: Male Female, but can't do the trick in the code. don't know how to change it.

tried with some code from w3schools given below, but that doesn't work too.

Tried radio type, but doesn't work

<label><?php _e( 'gender', 'themer' ); ?></label> <input type="radio" name="user_new_field" <?php if (isset($new_field) && $new_field=="female") echo "checked";?> value="female">Female <input type="radio" name="user_new_field" <?php if (isset($new_field) && $new_field=="male") echo "checked";?> value="male">Male </div>

i have to change the code to radio type, gonna use them in wordpress functions.php

 add_action( 'themer_after_user_profile_registration_fields_action', 'wpt_show_user_profile_custom_inputs' );
function wpt_show_user_profile_custom_inputs( $uid ) {
    $user_new_field = get_user_meta( $uid, 'user_new_field', true );
    $new_field = empty( $_POST['user_new_field'] ) ? $user_new_field : stripslashes( $_POST['user_new_field'] ); ?>
    <div class="field">
        <label><?php _e( 'gender', 'themer' ); ?></label>
        <input type="text" value="<?php echo $new_field; ?>" name="user_new_field" size="40" placeholder="<?php echo _x( 'My New Field Placeholder', 'Placeholder for: New field', 'themer' ); ?>" />
    </div>



<?php }

// Save the field value from user settings page
add_action( 'themer_user_profile_extra_fields_update', 'wpt_save_user_profile_custom_inputs' );
function wpt_save_user_profile_custom_inputs( $uid ) {
    if ( isset( $_POST['save-info'] ) ) {
        if ( isset( $_POST['user_new_field'] ) ) {
            update_user_meta( $uid, 'user_new_field', $_POST['user_new_field'] );
        }


    }
}
1
which theme you are using?Vel

1 Answers

0
votes

You can do this by replacing your code with below code in functions.php

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );

function extra_user_profile_fields( $user ) { ?>
    <h3><?php _e("Extra Field", "blank"); ?></h3>

    <table class="form-table">

    <tr>
    <th><label ><?php _e("Gender"); ?></label></th>
        <td>
            <input type="radio" name="gender" id="gender" value="male" <?php if(esc_attr( get_the_author_meta( 'gender', $user->ID ) ) == 'male'){echo ' checked '; } ?>class="regular-text" />Male
            <input type="radio" name="gender" id="gender" value="female" <?php if(esc_attr( get_the_author_meta( 'gender', $user->ID ) ) == 'female'){echo ' checked '; } ?> class="regular-text" />Female
            <br />
            <span class="description"><?php _e("Please enter your gender."); ?></span>
        </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, 'gender', $_POST['gender'] );
}