如何使wordpress管理员屏幕字段为必填项

时间:2018-10-04 17:38:08

标签: wordpress field

我想知道是否可以将管理区域(例如,用户页面上的字段)设置为必填或可选。

1 个答案:

答案 0 :(得分:0)

我在这里使用了参考网址https://www.cssigniter.com/how-to-add-a-custom-user-field-in-wordpress/


显示用户字段


如注册表格中所述,操作“ show_user_profile”和“ edit_user_profile”可用于添加我们自己的用户字段。前者在用户查看/编辑自己的个人资料信息时触发,而后者在用户(例如管理员)查看/编辑其他用户的个人资料时触发。这两个动作都将WP_User对象作为唯一参数传递。我们之前的代码已经使用了这些操作,是这样的:

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

function crf_show_extra_profile_fields( $user ) {   ?>
    <h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="year_of_birth"><?php esc_html_e( 'Year of birth', 'crf' ); ?></label></th>
            <td><?php echo esc_html( get_the_author_meta( 'year_of_birth', $user->ID ) ); ?></td>
        </tr>
    </table>
    <?php
} 

我们继续将纯文本的出生年份更改为输入元素,以便它可以接受用户输入。

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

function crf_show_extra_profile_fields( $user ) {
    $year = get_the_author_meta( 'year_of_birth', $user->ID );
    ?>
    <h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="year_of_birth"><?php esc_html_e( 'Year of birth', 'crf' ); ?></label></th>
            <td>
                <input type="number"
                   min="1900"
                   max="2017"
                   step="1"
                   id="year_of_birth"
                   name="year_of_birth"
                   value="<?php echo esc_attr( $year ); ?>"
                   class="regular-text"
                />
            </td>
        </tr>
    </table>
    <?php
}

让我们检查一下我们的个人资料页面:

enter image description here

相关问题