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
        }
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值