向数字表单添加自定义输入

时间:2022-02-05 作者:ali

如何将一个或多个输入添加到Digits 注册表单,并将其保存在元用户中?

1 个回复
SO网友:Monzur Alam

将代码粘贴到主题函数中。php。我希望它正在写注册表。

功能。php

// Displaying the field
function wp_registration_form() {
    $year = ! empty( $_POST[\'year_of_birth\'] ) ? intval( $_POST[\'year_of_birth\'] ) : \'\';
    ?>
    <p>
        <label for="year_of_birth"><?php esc_html_e( \'Year of birth\', \'wp\' ) ?><br/>
            <input type="number" min="1900" max="2017" step="1" id="year_of_birth" name="year_of_birth" value="<?php echo esc_attr( $year ); ?>"class="input"/>
        </label>
    </p>
    <?php
}
add_action( \'register_form\', \'wp_registration_form\' );

// Validating the field
function wp_registration_errors( $errors, $sanitized_user_login, $user_email ) {

    if ( empty( $_POST[\'year_of_birth\'] ) ) {
        $errors->add( \'year_of_birth_error\', __( \'<strong>ERROR</strong>: Please enter your year of birth.\', \'wp\' ) );
    }

    if ( ! empty( $_POST[\'year_of_birth\'] ) && intval( $_POST[\'year_of_birth\'] ) < 1900 ) {
        $errors->add( \'year_of_birth_error\', __( \'<strong>ERROR</strong>: You must be born after 1900.\', \'wp\' ) );
    }

    return $errors;
}
add_filter( \'registration_errors\', \'wp_registration_errors\', 10, 3 );

// Sanitizing and saving the field
function wp_user_register( $user_id ) {
    if ( ! empty( $_POST[\'year_of_birth\'] ) ) {
        update_user_meta( $user_id, \'year_of_birth\', intval( $_POST[\'year_of_birth\'] ) );
    }
}
add_action( \'user_register\', \'wp_user_register\' );

/**
 * Back end registration
 */

function wp_admin_registration_form( $operation ) {
    if ( \'add-new-user\' !== $operation ) {
        // $operation may also be \'add-existing-user\'
        return;
    }

    $year = ! empty( $_POST[\'year_of_birth\'] ) ? intval( $_POST[\'year_of_birth\'] ) : \'\';

    ?>
    <h3><?php esc_html_e( \'Personal Information\', \'wp\' ); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="year_of_birth"><?php esc_html_e( \'Year of birth\', \'wp\' ); ?></label> <span class="description"><?php esc_html_e( \'(required)\', \'wp\' ); ?></span></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
}
add_action( \'user_new_form\', \'wp_admin_registration_form\' );

// Validating the field
function wp_user_profile_update_errors( $errors, $update, $user ) {
    if ( $update ) {
        return;
    }

    if ( empty( $_POST[\'year_of_birth\'] ) ) {
        $errors->add( \'year_of_birth_error\', __( \'<strong>ERROR</strong>: Please enter your year of birth.\', \'wp\' ) );
    }

    if ( ! empty( $_POST[\'year_of_birth\'] ) && intval( $_POST[\'year_of_birth\'] ) < 1900 ) {
        $errors->add( \'year_of_birth_error\', __( \'<strong>ERROR</strong>: You must be born after 1900.\', \'wp\' ) );
    }
}
add_action( \'user_profile_update_errors\', \'wp_user_profile_update_errors\', 10, 3 );
add_action( \'edit_user_created_user\', \'wp_user_register\' );

// Profile display
function wp_show_extra_profile_fields( $user ) {
    ?>
    <h3><?php esc_html_e( \'Personal Information\', \'wp\' ); ?></h3>

    <table class="form-table">
        <tr>
            <th><label for="year_of_birth"><?php esc_html_e( \'Year of birth\', \'wp\' ); ?></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\', \'wp_show_extra_profile_fields\' );
add_action( \'edit_user_profile\', \'wp_show_extra_profile_fields\' );
输出屏幕:enter image description here

相关推荐

如何为多站点网络定制wp-signup.php?

我在google上搜索定制wp-signup.php 但没有发现运气。我需要的是:用户将注册</在注册过程中,用户将首先付款(如贝宝或信用卡)</用户成功付款时创建子域我的问题是:如何在中添加付款字段wp-signup.php? 我对wordpress真的很陌生。任何指南或链接都对我有很大帮助。登记表如下:用户名、电子邮件地址给我一个网站、付款、注册按钮