This post on Web Tips 钩住一些选项操作和过滤器来修改密码强度限额,但评论者似乎对此有一些问题,所以对此持保留态度。
// functions.php
add_action( \'user_profile_update_errors\', \'validateProfileUpdate\', 10, 3 );
add_filter( \'registration_errors\', \'validateRegistration\', 10, 3 );
add_action( \'validate_password_reset\', \'validatePasswordReset\', 10, 2 );
/**
* validate profile update
*
* @author Joe Sexton <[email protected]>
* @param WP_Error $errors
* @param boolean $update
* @param object $user raw user object not a WP_User
*/
public function validateProfileUpdate( WP_Error &$errors, $update, &$user ) {
return validateComplexPassword( $errors );
}
/**
* validate registration
*
* @author Joe Sexton <[email protected]>
* @param WP_Error $errors
* @param string $sanitized_user_login
* @param string $user_email
* @return WP_Error
*/
function validateRegistration( WP_Error &$errors, $sanitized_user_login, $user_email ) {
return validateComplexPassword( $errors );
}
/**
* validate password reset
*
* @author Joe Sexton <[email protected]>
* @param WP_Error $errors
* @param stdClass $userData
* @return WP_Error
*/
function validatePasswordReset( WP_Error &$errors, $userData ) {
return validateComplexPassword( $errors );
}
/**
* validate complex password
*
* @author Joe Sexton <[email protected]>
* @param WP_Error $errors
* @param stdClass $userData
* @return WP_Error
*/
function validateComplexPassword( $errors ) {
$password = ( isset( $_POST[ \'pass1\' ] ) && trim( $_POST[ \'pass1\' ] ) ) ? $_POST[ \'pass1\' ] : null;
// no password or already has password error
if ( empty( $password ) || ( $errors->get_error_data( \'pass\' ) ) )
return $errors;
// validate
if ( ! isStrongPassword( $password ) )
$errors->add( \'pass\', \'<strong>ERROR</strong>: Your password must contain at least 8 characters.\' ); // your complex password error message
return $errors;
}
/**
* isStrongPassword
*
* @author Joe Sexton <[email protected]>
* @param string $password
* @return boolean
*/
function isStrongPassword( $password ) {
return strlen( $password ) >= 8; // your complex password algorithm
}
如果您无法使用挂钩路线,或者您有自定义登录页面,
this post on Tuts Plus 有一个方便的教程,可以在您自己的表单上使用现有脚本。
添加到functions.php
:
wp_enqueue_script( \'password-strength-meter\' );
在HTML中:
<form>
<input type="password" name="password" />
<input type="password" name="password_retyped" />
<span id="password-strength"></span>
<input type="submit" disabled="disabled" value="Submit" />
</form>
脚本:
function checkPasswordStrength( $pass1,
$pass2,
$strengthResult,
$submitButton,
blacklistArray ) {
var pass1 = $pass1.val();
var pass2 = $pass2.val();
// Reset the form & meter
$submitButton.attr( \'disabled\', \'disabled\' );
$strengthResult.removeClass( \'short bad good strong\' );
// Extend our blacklist array with those from the inputs & site data
blacklistArray = blacklistArray.concat( wp.passwordStrength.userInputBlacklist() )
// Get the password strength
var strength = wp.passwordStrength.meter( pass1, blacklistArray, pass2 );
// Add the strength meter results
switch ( strength ) {
case 2:
$strengthResult.addClass( \'bad\' ).html( pwsL10n.bad );
break;
case 3:
$strengthResult.addClass( \'good\' ).html( pwsL10n.good );
break;
case 4:
$strengthResult.addClass( \'strong\' ).html( pwsL10n.strong );
break;
case 5:
$strengthResult.addClass( \'short\' ).html( pwsL10n.mismatch );
break;
default:
$strengthResult.addClass( \'short\' ).html( pwsL10n.short );
}
// The meter function returns a result even if pass2 is empty,
// enable only the submit button if the password is strong and
// both passwords are filled up
if ( 4 === strength && \'\' !== pass2.trim() ) {
$submitButton.removeAttr( \'disabled\' );
}
return strength;
}
jQuery( document ).ready( function( $ ) {
// Binding to trigger checkPasswordStrength
$( \'body\' ).on( \'keyup\', \'input[name=password1], input[name=password2]\',
function( event ) {
checkPasswordStrength(
$(\'input[name=password]\'), // First password field
$(\'input[name=password_retyped]\'), // Second password field
$(\'#password-strength\'), // Strength meter
$(\'input[type=submit]\'), // Submit button
[\'black\', \'listed\', \'word\'] // Blacklisted words
);
}
);
});