繁体   English   中英

请帮助将输入类型的文本从PHP代码更改为单选

[英]Kindly help to change input type text to radio from the PHP code

我试图将输入类型从文本更改为单选按钮,并将其更改为在Wordpress主题用户配置文件设置(如“性别:男女性”)中使用,但无法在代码中完成任务。 不知道如何更改它。

尝试使用下面给出的来自w3schools的一些代码,但这也不起作用。

尝试过无线电类型,但不起作用

<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>

我必须将代码更改为单选类型,然后在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'] );
        }


    }
}

您可以通过使用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'] );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM