簡體   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