将名字和姓氏添加到Wordpress注册表单

时间:2016-02-12 22:33:21

标签: php wordpress

我在WP上的注册表只有用户名,电子邮件和密码选项。

//1. firstname

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

$first_name = ( ! empty( $_POST['first_name'] ) ) ? trim( $_POST['first_name'] ) : '';

    ?>

    <?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'] ) );
    }
}

在我的注册表格(模板)中,我放置了以下内容:

                <label><?php _e('First Name', APP_TD) ?></label>
                <input tabindex="3" type="text" name="first_name" class="text regular-text" id="display_name" value="<?php echo esc_attr( wp_unslash( $first_name ) ); ?>" maxlength="100" />

它可以正常工作。它抓取名字并将其放在用户配置文件中。但是我不知道如何将姓氏添加到它,我是一个初学者,由于某种原因我无法得到姓氏。非常感谢帮助。

1 个答案:

答案 0 :(得分:4)

我从未在WP上使用类似的东西。你把这个代码放在哪里?

但是我会尝试执行这样的事情:

//1. Fistname

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'] ) : '';

?>

<?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' ) );
    }

    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']  ));
    }

    if ( ! empty( $_POST['last_name'] ) ) {
        update_user_meta( $user_id, 'last_name', trim( $_POST['last_name'] ) );
    }
}