注册表如何获得对其的全面控制

时间:2013-02-18 作者:bysanchy

感谢阅读。

我想完全控制在我的新网站上注册的方式。

我希望新用户编写自己的密码,并填写不同的字段。显示我自己的错误和消息等。。

你能给我指出正确的方向吗?我发现的只是使用短代码的插件,这些插件甚至与我想要实现的目标还不够接近。

2 个回复
最合适的回答,由SO网友:bysanchy 整理而成

使用Giri的功能和此链接中的信息http://codex.wordpress.org/Customizing_the_Registration_Form

我最终使用以下函数为我的WordPress站点创建了一个自定义注册。用户选择自己的密码,还指定姓名。用户名等于电子邮件,因为我不想要用户名。

在单独的文件(注册表表单所在的位置)中,检查表单是否已提交,并使用所需的所有参数调用此函数。然后处理错误并成功提交。

内部功能。php文件我添加了以下内容:

function custom_register_new_user( $user_login, $user_email, $user_apellidos, $user_nombre, $user_password, $user_repassword ) {
    $errors = new WP_Error();

    $sanitized_user_login = sanitize_user( $user_login );
    $user_email = apply_filters( \'user_registration_email\', $user_email );
    $user_nombre = escape($user_nombre);
    $user_password = escape($user_password);
    $user_repassword = escape($user_repassword);

    // Check the username
    if ( $sanitized_user_login == \'\' ) {
        $errors->add( \'empty_username\', __( \'Por favor escrib&iacute; un <strong>E-mail</strong>\' ) );
    } elseif ( ! validate_username( $user_login ) ) {
        $errors->add( \'invalid_username\', __( \'El <strong>e-mail</strong> es incorrecto\' ) );
        $sanitized_user_login = \'\';
    } elseif ( username_exists( $sanitized_user_login ) ) {
        $errors->add( \'username_exists\', __( \'Este <strong>e-mail</strong> ya est&aacute; registrado, eleg&iacute; otro\' ) );
    }

    // Check the e-mail address
    if ( $user_email == \'\' ) {
        $errors->add( \'empty_email\', \'Por favor escrib&iacute; un <strong>E-mail</strong>\' );
    } elseif ( ! is_email( $user_email ) ) {
        $errors->add( \'invalid_email\', \'El <strong>e-mail</strong> es incorrecto\' );
        $user_email = \'\';
    } elseif ( email_exists( $user_email ) ) {
        $errors->add( \'email_exists\', \'Este <strong>e-mail</strong> ya est&aacute; registrado, eleg&iacute; otro\' );
    }

    // Check the apellidos
    if ( $user_apellidos == \'\') {
        $errors->add( \'apellidos_error\', \'Por favor escrib&iacute; tus <strong>Apellidos</strong>\' );
    }

    // Check the nombre
    if ( $user_nombre == \'\') {
        $errors->add( \'nombre_error\', \'Por favor escrib&iacute; tu <strong>Nombre</strong>\' );
    }

    // Check the password
    if( $user_password == \'\' ) {
        $errors->add ( \'password_error\', \'Por favor escrib&iacute; una <strong>Contrase&ntilde;a</strong>\' );   
    }
    elseif( !(ctype_alnum($user_password)) ) {
        $errors->add ( \'password_error\', \'<strong>Contrase&ntilde;a</strong> no valida. Us&aacute; solo letras y n&uacute;meros\' ); 
    }
    elseif( $user_repassword == \'\' || $user_password != $user_password ) {
        $errors->add ( \'password_error\', \'Las <strong>Constrase&ntilde;as</strong> no coinciden</strong>\' );
    }



    do_action( \'register_post\', $sanitized_user_login, $user_email, $errors );

    $errors = apply_filters( \'registration_errors\', $errors, $sanitized_user_login, $user_email, $user_nombre, $user_apellidos, $user_password );

    if ( $errors->get_error_code() )
        return $errors;

    $user_pass = $user_password;
    $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
    if ( ! $user_id ) {
        $errors->add( \'registerfail\', sprintf( __( \'<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !\' ), get_option( \'admin_email\' ) ) );
        return $errors;
    }

    update_user_meta($user_id, \'first_name\', $user_nombre );
    update_user_meta($user_id, \'last_name\', $user_apellidos );

    /* wp_new_user_notification( $user_id, $user_pass ); */

    return $user_id;
}
要处理错误,请检查函数的返回值是否为\\u wp\\u error。否则,请继续发送欢迎信息或其他内容。

$return = custom_register_new_user($_POST[\'email\'], $_POST[\'email\'], $_POST[\'apellidos\'], $_POST[\'nombre\'], $_POST[\'password\'], $_POST[\'repassword\']);

                    if( is_wp_error($return) ) {

                        $errores = $return->errors;
                        if( count($errores) > 0 ) {

                            if( $errores[\'apellidos_error\'] )
                                $error_apellidos = $errores[\'apellidos_error\'][0];

                            if( $errores[\'nombre_error\'] )
                                $error_nombre = $errores[\'nombre_error\'][0];

                                ....
                                etc
                                .... 
此外,在Deafolt注册表中添加了“Header”重定向,因此用户将始终登录到我的自定义表单上。登录和忘记密码相同

SO网友:PrivateUser

复制此功能并根据需要进行修改。

function custom_register_new_user( $user_login, $user_email ) {
    $errors = new WP_Error();

    $sanitized_user_login = sanitize_user( $user_login );
    $user_email = apply_filters( \'user_registration_email\', $user_email );

    // Check the username
    if ( $sanitized_user_login == \'\' ) {
        $errors->add( \'empty_username\', __( \'<strong>ERROR</strong>: Please enter a username.\' ) );
    } elseif ( ! validate_username( $user_login ) ) {
        $errors->add( \'invalid_username\', __( \'<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.\' ) );
        $sanitized_user_login = \'\';
    } elseif ( username_exists( $sanitized_user_login ) ) {
        $errors->add( \'username_exists\', __( \'<strong>ERROR</strong>: This username is already registered. Please choose another one.\' ) );
    }

    // Check the e-mail address
    if ( $user_email == \'\' ) {
        $errors->add( \'empty_email\', __( \'<strong>ERROR</strong>: Please type your e-mail address.\' ) );
    } elseif ( ! is_email( $user_email ) ) {
        $errors->add( \'invalid_email\', __( \'<strong>ERROR</strong>: The email address isn&#8217;t correct.\' ) );
        $user_email = \'\';
    } elseif ( email_exists( $user_email ) ) {
        $errors->add( \'email_exists\', __( \'<strong>ERROR</strong>: This email is already registered, please choose another one.\' ) );
    }

    do_action( \'register_post\', $sanitized_user_login, $user_email, $errors );

    $errors = apply_filters( \'registration_errors\', $errors, $sanitized_user_login, $user_email );

    if ( $errors->get_error_code() )
        return $errors;

    $user_pass = wp_generate_password( 12, false);
    $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
    if ( ! $user_id ) {
        $errors->add( \'registerfail\', sprintf( __( \'<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !\' ), get_option( \'admin_email\' ) ) );
        return $errors;
    }

    update_user_option( $user_id, \'default_password_nag\', true, true ); //Set up the Password change nag.

    wp_new_user_notification( $user_id, $user_pass );

    return $user_id;
}
Read this page 显示您自己的错误消息。

相关问题:

Front-end Register Form

How to display WordPress User Registration Form in front end of the website?

结束

相关推荐