Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I´m working on a front end registration form adn have gotten as far as getting the standard registration form to work, but... I´d like to add the oportunity to let the user choose his/her own password.

I´m a bit noob at this so could anyone plese help me? :)

Here is my code so far:

<?php if(!is_user_logged_in()) { ?>


<?php
if(get_option('users_can_register')) {
//Check whether user registration is enabled by the administrator
?>

<?php

if($_POST){
    //We shall SQL escape all inputs
    $username = $wpdb->escape($_REQUEST['username']);
    if(empty($username)) {
        echo "<span style='color:#FF0000'><strong>Error..</strong></span><br /><br />You have to fill in the username.";
        exit();
    }
    $email = $wpdb->escape($_REQUEST['email']);
    if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)) {
        echo "<span style='color:#FF0000'><strong>Error..</strong></span><br /><br />please use a calid e-mailadress.";
        exit();
    }

    $random_password = wp_generate_password( 12, false );
    $status = wp_create_user( $username, $random_password, $email );
    if ( is_wp_error($status) )
        echo "<span style='color:#FF0000'><strong>Feil..</strong></span><br /><br />Username allready exist. please try another one.";
    else {
        $from = get_option('admin_email');
        $headers = 'From: '.$from . "\r\n";
        $subject = "Registration ok!";
        $msg = "Welcome, you are now registered. Here is your username and password.\Info:\Username: $username\Password: $random_password";
        wp_mail( $email, $subject, $msg, $headers );
        echo "<strong>You are now registered. An e-mail is now sent to you with your username and password..";
    }

    exit();

}
 else
{
//Embed the register form and javascript here

?>


<div id="result"></div>
<div style="padding-top:5px; padding-bottom:10px;">Fill in your info to register.</div>

<form id="wp_signup_form" action="" method="post">
<input type="text" name="username" style="width:250px; margin-bottom:3px;" placeholder="Username"><br />
<input type="text" name="email" placeholder="E-mail" style="width:250px; margin-bottom:3px;"><br />
<input type="submit" id="submitbtn" name="submit" value="Register" class="knapp" style="padding:8px;">
</form>
<script type="text/javascript">                         
$("#submitbtn").click(function() {
$('#result').html('<img src="<?php bloginfo('template_url'); ?>/images/loader.gif" class="loader" />').fadeIn();
var input_data = $('#wp_signup_form').serialize();
$.ajax({
type: "POST",
url:  "<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>",
data: input_data,
success: function(msg){
$('.loader').remove();
$('
<div>').html(msg).appendTo('div#result').hide().fadeIn('slow');
}
});
return false;

});
</script>
<?php
// embedded

}
?>

<?php
    }

else 
?>

<!-- regskjema end -->
<br />
Allready registered? Login to your account with the form in the sidebar.
<?php } else { ?>
You are allready logged in...
<?php } ?>
share|improve this question

1 Answer 1

up vote 9 down vote accepted

Its Not has hard as you think it is :)

Add the password fields to your form :

password: <input type="password" name="pass1" style="width:250px; margin-bottom:3px;"><br />
repeat password: <input type="password" name="pass2" style="width:250px; margin-bottom:3px;"><br />

then in your if($_POST){ replace this line:

$random_password = wp_generate_password( 12, false );

with this:

$pass1 = $wpdb->escape($_REQUEST['pass1']);
$pass2 = $wpdb->escape($_REQUEST['pass2']);
if ($pass1 != $pass2){
    echo "<span style='color:#FF0000'><strong>Error..</strong></span><br /><br />please use a passwords don't match.";
        exit();

}
$random_password = $pass1;
share|improve this answer
2  
nice snippet mate - thanks. –  Sagive SEO Sep 9 '12 at 11:01
    
works like a charm! –  Demilio Sep 9 '12 at 12:14
1  
It also worked for me, so +1. –  Ciprian Mar 10 '13 at 12:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.