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

Llo*_*oyd 3 php wordpress

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

//1. firstname
Run Code Online (Sandbox Code Playgroud)

add_action( 'register_form', 'myplugin_register_form' ); 函数 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'] ) );
    }
}
Run Code Online (Sandbox Code Playgroud)

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

                <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" />
Run Code Online (Sandbox Code Playgroud)

它按其应有的方式工作。它获取名字并将其放入用户配置文件中。但是我不确定如何添加姓氏,我是一个初学者,并且由于某种原因我无法让姓氏起作用。非常感谢您的帮助。

Mym*_*aaa 5

我从来没有在我的 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'] ) );
    }
}
Run Code Online (Sandbox Code Playgroud)