钩子文档

random_password

💡 云策文档标注

概述

random_password 是一个 WordPress 过滤器,用于修改随机生成的密码。它允许开发者自定义密码生成逻辑,例如添加额外字符或调整密码结构。

关键要点

  • 过滤器名称:random_password,用于过滤 wp_generate_password() 函数生成的密码。
  • 参数:$password(生成的密码字符串)、$length(密码长度)、$special_chars(是否包含标准特殊字符)、$extra_special_chars(是否包含其他特殊字符)。
  • 用法:通过 add_filter() 钩子添加自定义函数来修改密码,例如在密码后追加自定义字符串。
  • 版本历史:从 WordPress 3.0.0 引入,5.3.0 版本添加了 $length、$special_chars 和 $extra_special_chars 参数。

代码示例

add_filter( 'random_password', 'my_random_password' );

function my_random_password() {
    $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $length = 10;
    $password = '';
    for( $i = 0; $i 

📄 原文内容

Filters the randomly-generated password.

Parameters

$passwordstring
The generated password.
$lengthint
The length of password to generate.
$special_charsbool
Whether to include standard special characters.
$extra_special_charsbool
Whether to include other special characters.

Source

return apply_filters( 'random_password', $password, $length, $special_chars, $extra_special_chars );

Changelog

Version Description
5.3.0 Added the $length, $special_chars, and $extra_special_chars parameters.
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    Example migrated from Codex:

    Form a new password by appending your own password string to the generated password.

    add_filter( 'random_password', 'my_random_password' );
    
    function my_random_password() {
        $characters ='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $length = 10;
        $password = '';
        for( $i = 0; $i < $length; $i++ ) {
            $password .= substr( $characters , wp_rand( 0, strlen( $characters ) - 1 ), 1 );
        }
        return $password;
    }