是否更改密码强度指示器名称?

时间:2015-08-26 作者:LBF

是否可以更改“弱、中、强”等标签。。。在用户配置文件中使用的密码指示器中?我被要求将单词“弱”改为“好”,因为我们的订阅者可以接受这种级别的密码。有我可以挂上的过滤器吗?

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

我能用这种方法修复它。这是好的还是坏的?这是可行的,但我对反馈很感兴趣,以防这可能会产生意想不到的后果。

add_filter(\'gettext\', \'translate_text\'); 
add_filter(\'ngettext\', \'translate_text\');

function translate_text($translated) { 
$translated = str_ireplace(\'Very Weak\', \'Bad\', $translated); 
$translated = str_ireplace(\'Weak\', \'OK\', $translated); 

return $translated; 
}

SO网友:dragoweb

将此添加到我的函数中。子主题文件夹中的php文件为我做到了这一点:

add_action( \'wp_enqueue_scripts\', \'my_strength_meter_localize_script\' );
function my_strength_meter_localize_script() {
    wp_localize_script( \'password-strength-meter\', \'pwsL10n\', array(
        \'empty\'    => __( \'But... it\\\'s empty!\', \'theme-domain\' ),
        \'short\'    => __( \'Too short!\', \'theme-domain\' ),
        \'bad\'      => __( \'Not even close!\', \'theme-domain\' ),
        \'good\'     => __( \'You are getting closer...\', \'theme-domain\' ),
        \'strong\'   => __( \'Now, that\\\'s a password!\', \'theme-domain\' ),
        \'mismatch\' => __( \'They are completely different, come on!\', \'theme-domain\' )
    ) );
}

Source

SO网友:feelinferrety

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
            );
        }
    );
});

SO网友:czerspalace

“弱”文本被传递_x 函数,该函数调用translate_with_gettext_context, 因此,我将尝试以下方法:

add_filter( \'gettext_with_context\', \'wpse199813_change_password_indicatior\', 10, 4 );

function wpse199813_change_password_indicatior($translations, $text, $context, $domain){

    if( $text == "Weak" && $context == "password strength")
        return "OK";

    return $translations;
}

SO网友:Max Yudin

您可以在主题本地化文件中正确执行。

打开*.pot 归档并创建所需标签的翻译,例如。OK 对于Weak.

看见password-strength-meter 绕线#366 在里面/wp-includes/script-loader.php 供参考(但不要更改任何内容)。

Update:

我已经找到了使用jQuery解决类似问题的解决方案,但您必须简化代码,只使用一个密码表:How to use wordpress default Password Strength Meter script. 另请参见/wp-admin/js/user-profile.js 供参考,因为该解决方案非常古老。