Skip to content

Instantly share code, notes, and snippets.

@viruthagiri
Forked from trepmal/pw-at-signup.php
Created February 7, 2012 18:27
Show Gist options
  • Save viruthagiri/1761111 to your computer and use it in GitHub Desktop.
Save viruthagiri/1761111 to your computer and use it in GitHub Desktop.
WordPress: Choose password at registration (Multisite)
<?php
//Plugin Name: Choose password at registration (Multisite)
//Description: Doesn't change the confirmation screen or email
add_action('signup_extra_fields', 'ask_for_password');
function ask_for_password( $errors ) {
if ( $errmsg = $errors->get_error_message('bad_password') ) {
echo '<p class="error">'.$errmsg.'</p>';
}
?> <p>You can set your password here. If left blank, a random one will be generated for you.</p>
<label>Password:</label><input type='password' name='password1' />
<label>Password again:</label><input type='password' name='password2' />
<?php
}
add_filter( 'wpmu_validate_user_signup', 'verify_password' );
function verify_password( $result ) {
$password1 = $_POST['password1'];
$password2 = $_POST['password2'];
//don't require a password
//if empty, let WP use generated password
// if ( empty( $password1 ) ) {
// $result['errors']->add('bad_password', 'Please provide a password');
// }
if ( $password1 != $password2 ) {
$result['errors']->add('bad_password', 'Passwords do not match');
}
if ( strlen( $password1 ) > 0 && strlen( $password1 ) < 8 ) {
$result['errors']->add('bad_password', 'Password is too short');
}
return $result;
}
add_filter( 'add_signup_meta', 'save_password_in_signup_meta' );
function save_password_in_signup_meta( $meta ) {
$password1 = $_POST['password1'];
$meta['password'] = $password1;
return $meta;
}
add_action('wpmu_activate_user', 'apply_password_during_activation', 10, 3 );
function apply_password_during_activation( $user_id, $password, $meta ) {
//if password was provided, use that
if ( strlen( $meta['password'] ) > 0)
wp_set_password( $meta['password'], $user_id );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment