如何使用内置的wp-Register()模板标记制作定制的用户注册表单

时间:2016-04-10 作者:femi jeffery

我想在WordPress中制作一个注册表,但我需要它包含我自己的自定义输入字段。

例如,我需要它有一个输入字段,如国家、学校、课程、入学年份、年龄等。

提前感谢您的帮助。

1 个回复
最合适的回答,由SO网友:darrinb 整理而成

《食品法典》有一篇文章概述了如何做到这一点:https://codex.wordpress.org/Customizing_the_Registration_Form

它甚至提供了一个示例:

//1. Add a new form element...
add_action( \'register_form\', \'myplugin_register_form\' );

function myplugin_register_form() {
    $first_name = ( ! empty( $_POST[\'first_name\'] ) ) ? trim( $_POST[\'first_name\'] ) : \'\';
    ?>
    <p>
        <label for="first_name"><?php _e( \'First Name\', \'mydomain\' ) ?><br />
        <input type="text" name="first_name" id="first_name" class="input" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" size="25" /></label>
    </p>
    <?php
}

//2. Add validation. In this case, we make sure first_name is required.
add_filter( \'registration_errors\', \'myplugin_registration_errors\', 10, 3 );

function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {

    if ( empty( $_POST[\'first_name\'] ) || ! empty( $_POST[\'first_name\'] ) && trim( $_POST[\'first_name\'] ) == \'\' ) {
        $errors->add( \'first_name_error\', __( \'<strong>ERROR</strong>: You must include a first name.\', \'mydomain\' ) );
    }

    return $errors;
}

//3. Finally, save our extra registration user meta.
add_action( \'user_register\', \'myplugin_user_register\' );

function myplugin_user_register( $user_id ) {
    if ( ! empty( $_POST[\'first_name\'] ) ) {
        update_user_meta( $user_id, \'first_name\', trim( $_POST[\'first_name\'] ) );
    }
}

相关推荐

如何让`wp-list-table`显示我在Custom-Post中的`Custom-Fields`

一切都好吗<我需要wp-list-table 也要显示custom-fields 在每个custom-post 我有,但我不知道如何做到这一点,在这幅图中,它显示了带有字段的表格:Title, Author and Publication Date: 我想要的是能够选择custom-fields 将出现,例如以下示例Title, Carta, Naipe, Author, and Date of Publication: