用户注册后自定义成功消息

时间:2020-11-14 作者:jilian

我将此代码用作自定义注册表单。是否有机会为其创建成功消息?

<?php
/*
** Template Name: Custom Register Page
*/
get_header();

global $wpdb, $user_ID;  

    if (isset($_POST[\'user_registeration\']))
    {
        //registration_validation($_POST[\'username\'], $_POST[\'useremail\']);
        global $reg_errors;
        $reg_errors = new WP_Error;
        $username=$_POST[\'username\'];
        $useremail=$_POST[\'useremail\'];
        $password=$_POST[\'password\'];
        
        
        if(empty( $username ) || empty( $useremail ) || empty($password))
        {
            $reg_errors->add(\'field\', \'Required form field is missing\');
        }    
        if ( 6 > strlen( $username ) )
        {
            $reg_errors->add(\'username_length\', \'Username too short. At least 6 characters is required\' );
        }
        if ( username_exists( $username ) )
        {
            $reg_errors->add(\'user_name\', \'The username you entered already exists!\');
        }
        if ( ! validate_username( $username ) )
        {
            $reg_errors->add( \'username_invalid\', \'The username you entered is not valid!\' );
        }
        if ( !is_email( $useremail ) )
        {
            $reg_errors->add( \'email_invalid\', \'Email id is not valid!\' );
        }
        
        if ( email_exists( $useremail ) )
        {
            $reg_errors->add( \'email\', \'Email Already exist!\' );
        }
        if ( 5 > strlen( $password ) ) {
            $reg_errors->add( \'password\', \'Password length must be greater than 5!\' );
        }
        
        if (is_wp_error( $reg_errors ))
        { 
            foreach ( $reg_errors->get_error_messages() as $error )
            {
                 $signUpError=\'<p style="color:#FF0000; text-aling:left;"><strong>ERROR</strong>: \'.$error . \'<br /></p>\';
            } 
        }
        
        
        if ( 1 > count( $reg_errors->get_error_messages() ) )
        {
            // sanitize user form input
            global $username, $useremail;
            $username   =   sanitize_user( $_POST[\'username\'] );
            $useremail  =   sanitize_email( $_POST[\'useremail\'] );
            $password   =   esc_attr( $_POST[\'password\'] );
            
            $userdata = array(
                \'user_login\'    =>   $username,
                \'user_email\'    =>   $useremail,
                \'user_pass\'     =>   $password,
                );
            $user = wp_insert_user( $userdata );
        }
        
    
    } 
    

?>


<h3>Create your account</h3>
<form action="" method="post" name="user_registeration">
    <label>Username <span class="error">*</span></label>
    <input type="text" name="username" placeholder="Enter Your Username" class="text" required /><br />
    <label>Email address <span class="error">*</span></label>
    <input type="text" name="useremail" class="text" placeholder="Enter Your Email" required /> <br />
    <label>Password <span class="error">*</span></label>
    <input type="password" name="password" class="text" placeholder="Enter Your password" required /> <br />
    <input type="submit" name="user_registeration" value="SignUp" />
</form>
<?php if(isset($signUpError)){echo \'<div>\'.$signUpError.\'</div>\';}?>

2 个回复
SO网友:Gregory

一种简单的方法是重定向表单提交者

// when form is submitted :
wp_redirect($your_post_url . \'&submitform=1\');
exit;
然后你可以测试一下;“提交格式”;定义为显示所需内容:

// test submitform url setting :
if (isset($_GET[\'submitform\'])) 
    echo \'<div class="successmessage"><p>\' . __(\'Your Success message.\') . \'</p></div>\';
endif;

SO网友:Bysander

如果你看看函数wp_insert_user() in the codex 如果一切顺利,您将看到它返回新的用户ID,或者WP Error Object 如果失败。

因此,您可以将表单包装成条件以处理不同的场景。

(Shorthand syntax)
if( is_numeric( $user ) ) {
    <p>Congratualtions on being a new user</p>
} else if( is_wp_error( $user ) ) {
    <p>There\'s been a problem adding the user</p>
} else {
    <form.........
    // Don\'t forget the sign up error handling too
}
我还要补充一点,在您最初提供的代码中,我可以看到您正在检查可能不是通过插入DB的最终值的值wp_insert_user - 如果要插入经过清理的用户字符串,请检查经过清理的用户字符串。否则this<tag>user 添加为thisuser - 但你没有检查。。。。

相关推荐

如何使wp-signup.php成为一个1步流程

我正在为房地产经纪人开发一个多站点。Wp注册。php从我的主题中使用了错误的页面模板(主页而不是全宽页面模板),所以我从wp注册中获取了一些重要信息。php创建了一个页面模板。结果如下:[http://thewebrockstar.com/realestate/signup/][1]然而,现在第2步(在点击下一步之后)将从我的wp注册页面模板重定向到原来的wp注册页面。php。有什么解决办法吗?我在想注册wp是最容易的。php是一个一步流程(所有内容都在一个页面上),而不是:http://i.stack.