如何在默认注册表中添加名字和姓氏?

时间:2017-03-26 作者:Archangel17

是否可以添加名字(&A);wordpress默认注册表的姓氏?

3 个回复
最合适的回答,由SO网友:Faysal Mahamud 整理而成

在函数中添加此代码。php

add_action( \'register_form\', \'myplugin_register_form\' );
function myplugin_register_form() {

    $first_name = ( ! empty( $_POST[\'first_name\'] ) ) ? trim( $_POST[\'first_name\'] ) : \'\';
    $last_name = ( ! empty( $_POST[\'last_name\'] ) ) ? trim( $_POST[\'last_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>

        <p>
            <label for="last_name"><?php _e( \'Last Name\', \'mydomain\' ) ?><br />
                <input type="text" name="last_name" id="last_name" class="input" value="<?php echo esc_attr( wp_unslash( $last_name ) ); ?>" size="25" /></label>
        </p>

        <?php
    }

    //2. Add validation. In this case, we make sure first_name and last_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\' ) );
        }
        if ( empty( $_POST[\'last_name\'] ) || ! empty( $_POST[\'last_name\'] ) && trim( $_POST[\'last_name\'] ) == \'\' ) {
            $errors->add( \'last_name_error\', __( \'<strong>ERROR</strong>: You must include a last 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\'] ) );
            update_user_meta( $user_id, \'last_name\', trim( $_POST[\'last_name\'] ) );
        }
    }
有关更多信息,请参阅codex

你也可以使用一些插件

cimy-user-extra-fields

Custom Registration Form

User Registration Aide

SO网友:Ignasi Calvo

代码正常,但我发现了一个小bug。在验证last\\u name时,if条件中的最后一个var具有“first\\u name”,并且必须具有“last\\u name”。因此,正确地说,这段代码应该是:

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

SO网友:Ethan Root

$form[\'account\'][\'lname\'] = array(\'#type\' => \'textfield\',
\'#title\' => t(\'LastName\'),
\'#default_value\' => $edit[\'LastName\'],
\'#maxlength\' => LASTNAME_MAX_LENGTH,
\'#description\' => t(\'Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.\'),
\'#required\' => TRUE,
);
您只能在UI中执行此操作

导航到sitedomain/admin/config/people/accounts/fields 并将名字、姓氏添加为字段,它将显示默认注册表中的名字和姓氏