【问题标题】:Kindly help to change input type text to radio from the PHP code请帮助将输入类型文本从 PHP 代码更改为收音机
【发布时间】:2019-08-14 03:56:53
【问题描述】:

我正在尝试添加输入类型并将其从文本更改为单选按钮,以在我的 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'] );
        }


    }
}

【问题讨论】:

  • 您使用的是哪个主题?

标签: php html wordpress custom-fields


【解决方案1】:

你可以通过在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'] );
}

【讨论】:

    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    相关资源
    最近更新 更多