繁体   English   中英

如何将自定义字段添加到注册表单? WordPress的

[英]How can i add custom fields to registration form ? wordpress

如何向此注册码添加自定义字段? 例如,我要添加“公司名称”。 在这种形式下,我们只有基本字段。

我的代码:

<div class="registration">
            <form name="registerform" action="<?php echo site_url('wp-login.php?action=register', 'login_post') ?>" method="post">
                <p>
                    <label for="user_login">Username</label>
                    <input type="text" name="user_login" value="">
                </p>
                <p>
                    <label for="user_email">E-mail</label>
                    <input type="text" name="user_email" id="user_email" value="">
                </p>
                <p style="display:none">
                    <label for="confirm_email">Please leave this field empty</label>
                    <input type="text" name="confirm_email" id="confirm_email" value="">
                </p>

                <p id="reg_passmail">A password will be e-mailed to you.</p>

                <input type="hidden" name="redirect_to" value="/login/?action=register&success=1" />
                <p class="submit"><input type="submit" name="wp-submit" id="wp-submit" value="Register" /></p>
            </form>
        </div>

此代码将添加一个名为“公司名称”的自定义字段,并使其强制注册。 您可以在线遵循WordPress 自定义注册表格。

function myplugin_add_registration_fields() {

    //Get and set any values already sent
    $company_name = ( isset( $_POST['company_name'] ) ? $_POST['company_name'] : '' );
    ?>

    <p>
        <label for="company_name"><?php _e( 'Company Name', 'myplugin_textdomain' ) ?><br />
            <input type="text" name="company_name" id="company_name" class="input" value="<?php echo esc_attr( stripslashes( $company_name ) ); ?>" size="50" /></label>
    </p>

    <?php
}

add_action( 'register_form', 'myplugin_add_registration_fields' );

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

    if ( empty ( $_POST['company_name'] ) ) {
        $errors->add( 'company_name_error', __( '<strong>ERROR</strong>: Company name is required.', 'my_textdomain' ) );
    }

    return $errors;
}

add_filter( 'registration_errors', 'myplugin_check_fields', 10, 3 );


function myplugin_registration_save( $user_id ) {

    if ( isset( $_POST['company_name'] ) )
        update_user_meta($user_id, 'company_name', $_POST['company_name']);

}

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM