wordpress 给原生注册页面添加自定义字段

在不使用 wordpress 插件的情况下,我们可以修改 wordpress 的原生注册页面添加自定义字段.

示例 : 比如我门需要添加密码和确认密码输入框,让注册用户输入密码,注册成功后给用户发送邮件

示例代码:

//给注册页面添加 :密码输入、本站站名验证
    add_action( 'register_form', 'ts_show_extra_register_fields' );
    add_action( 'register_form', 'ts_show_extra_register_fields' );
    function ts_show_extra_register_fields(){
        ?>
        <p>
            <label for="password">Password<br/>
                <input id="password" class="input" type="password" tabindex="30" size="25" value="" name="password" />
            </label>
        </p>
        <p>
            <label for="repeat_password">Confirm password<br/>
                <input id="repeat_password" class="input" type="password" tabindex="40" size="25" value="" name="repeat_password" />
            </label>
        </p>
        <?php
    }


    // 注册错误提示
    add_action( 'register_post', 'ts_check_extra_register_fields', 10, 3 );
    function ts_check_extra_register_fields($login, $email, $errors) {
        if ( $_POST['password'] !== $_POST['repeat_password'] ) {
            $errors->add( 'passwords_not_matched', "<strong>Error: </strong>Entered passwords differ" );
        }
    }


    // 注册成功写库
    add_action( 'user_register', 'ts_register_extra_fields' );
    function ts_register_extra_fields( $user_id ){
        $userdata = array();
        $userdata['ID'] = $user_id;
        if ( $_POST['password'] !== '' ) {
            $userdata['user_pass'] = $_POST['password'];
        }
        $new_user_id = wp_update_user( $userdata );

        // 发送邮件
        $custom_subject = "Aragon Tourism: Registered Successfully";
        $msg = "Congratulations, you've been registered successfully on Aragon Tourism Site!". "\r\n\r\n";
        $msg .= "Username:" .$_POST['user_login'];

        wp_mail($_POST['user_email'],$custom_subject,$msg);

    }


    add_action('phpmailer_init', 'wse199274_intercept_registration_email');
    function wse199274_intercept_registration_email($phpmailer){
        $admin_email = get_option( 'admin_email' );

        # Intercept username and password email by checking subject line
        if( strpos($phpmailer->Subject, 'Your username and password info') ){
            # clear the recipient list
            $phpmailer->ClearAllRecipients();
            # optionally, send the email to the WordPress admin email
            $phpmailer->AddAddress($admin_email);
        }else{
            #not intercepted
        }
    }

    add_action('phpmailer_init', 'wse19927411_intercept_registration_email');
    function wse19927411_intercept_registration_email($phpmailer){
        $admin_email = get_option( 'admin_email' );

        # Intercept username and password email by checking subject line
        if( strpos($phpmailer->Subject, 'Notice of Password Change') ){
            # clear the recipient list
            $phpmailer->ClearAllRecipients();
            # optionally, send the email to the WordPress admin email
            $phpmailer->AddAddress($admin_email);
        }else{
            #not intercepted
        }
    }

发布了145 篇原创文章 · 获赞 24 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/xiaobinqt/article/details/83538607