如何在自定义注册表中调用或添加密码输入/生成密码/密码强度计?

时间:2018-04-30 作者:Manyang

我将分享我如何使用默认/标准wordpress密码添加密码输入/生成密码/密码强度表的知识。如果你喜欢这些信息,请投票支持我的问题和答案。

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

您可以使用简单的代码在管理区或前端区的任何自定义注册表中添加密码输入/生成密码/密码强度表。

第一步:在注册表中添加此html代码

<table class="form-table">
  <tr id="password" class="user-pass1-wrap">
    <th><label for="pass1"><?php _e( \'New Password\' ); ?></label></th>
    <td>
        <input class="hidden" value=" " /><!-- #24364 workaround -->
        <button type="button" class="button wp-generate-pw hide-if-no-js"><?php _e( \'Generate Password\' ); ?></button>
        <div class="wp-pwd hide-if-js">
            <span class="password-input-wrapper">
                <input type="password" name="pass1" id="pass1" class="regular-text" value="" autocomplete="off" data-pw="<?php echo esc_attr( wp_generate_password( 24 ) ); ?>" aria-describedby="pass-strength-result" />
            </span>
            <button type="button" class="button wp-hide-pw hide-if-no-js" data-toggle="0" aria-label="<?php esc_attr_e( \'Hide password\' ); ?>">
                <span class="dashicons dashicons-hidden"></span>
                <span class="text"><?php _e( \'Hide\' ); ?></span>
            </button>
            <button type="button" class="button wp-cancel-pw hide-if-no-js" data-toggle="0" aria-label="<?php esc_attr_e( \'Cancel password change\' ); ?>">
                <span class="text"><?php _e( \'Cancel\' ); ?></span>
            </button>
            <div style="display:none" id="pass-strength-result" aria-live="polite"></div>
        </div>
    </td>
  </tr>
  <tr class="user-pass2-wrap hide-if-js">
    <th scope="row"><label for="pass2"><?php _e( \'Repeat New Password\' ); ?></label></th>
    <td>
    <input name="pass2" type="password" id="pass2" class="regular-text" value="" autocomplete="off" />
    <p class="description"><?php _e( \'Type your new password again.\' ); ?></p>
    </td>
  </tr>
  <tr class="pw-weak">
    <th><?php _e( \'Confirm Password\' ); ?></th>
    <td>
        <label>
            <input type="checkbox" name="pw_weak" class="pw-checkbox" />
            <span id="pw-weak-text-label"><?php _e( \'Confirm use of potentially weak password\' ); ?></span>
        </label>
    </td>
  </tr>
</table>

<script type="text/javascript">
    if (window.location.hash == \'#password\') {
        document.getElementById(\'pass1\').focus();
    }
</script>
在那之后enqueue 2 wordpress用户管理脚本。

wp_enqueue_script( \'password-strength-meter\' );
wp_enqueue_script( \'user-profile\' );
如果你把它放在管理区,你可以使用这个脚本

add_action( \'admin_enqueue_scripts\', \'include_password_in_admin_page\' );
function include_password_in_admin_page($hook){
    if ( \'your_screen_admin_page\' != $hook ) {
       return;
    }
    wp_enqueue_script( \'password-strength-meter\' );
    wp_enqueue_script( \'user-profile\' );
}
如果您将此代码放在特定页面中custom page template 在前端,您可以使用此脚本

function enqueue_password_script(){
  wp_enqueue_script( \'password-strength-meter\' );
  wp_enqueue_script( \'user-profile\' );
}
add_action( \'template_redirect\', \'include_password_in_specific_template_page\' );
function include_password_in_specific_template_page(){
    $pageTemp = get_page_template_slug();
    $pageArray = array(\'page-specific.php\');//add every page template you need to add password script
    if ( in_array($pageTemp, $pageArray) ) {
         add_action( \'wp_enqueue_scripts\', \'enqueue_password_script\');
    }
}
如果您需要标准wordpress样式,只需添加wp_enqueue_style(\'forms\');

结束

相关推荐

Password Protecting Media

是否可以对我们网站的访问者(非用户)的PDF进行密码保护?我们已将PDF上传到媒体,并在页面上创建了指向该PDF的链接,但我们只想对该PDF进行密码保护,而不是对包含指向该PDF的链接的整个页面进行密码保护。