返回介绍

wp_generate_password()

发布于 2017-09-11 11:57:59 字数 4613 浏览 1142 评论 0 收藏 0

wp_generate_password( int $length = 12,  bool $special_chars = true,  bool $extra_special_chars = false )

Generates a random password drawn from the defined set of characters.


description


参数

$length

(int) (Optional) The length of password to generate.

Default value: 12

$special_chars

(bool) (Optional) Whether to include standard special characters.

Default value: true

$extra_special_chars

(bool) (Optional) Whether to include other special characters. Used when generating secret keys and salts.

Default value: false


返回值

(string) The random password.


源代码

File: wp-includes/pluggable.php

function wp_generate_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
	$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	if ( $special_chars )
		$chars .= '!@#$%^&*()';
	if ( $extra_special_chars )
		$chars .= '-_ []{}<>~`+=,.;:/?|';

	$password = '';
	for ( $i = 0; $i < $length; $i++ ) {
		$password .= substr($chars, wp_rand(0, strlen($chars) - 1), 1);
	}

	/**
	 * Filters the randomly-generated password.
	 *
	 * @since 3.0.0
	 *
	 * @param string $password The generated password.
	 */
	return apply_filters( 'random_password', $password );
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/pluggable.php: wp_rand()
  • wp-includes/pluggable.php: random_password
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-includes/embed.php: wp_filter_oembed_result()
  • wp-includes/user.php: get_password_reset_key()
  • wp-admin/includes/ajax-actions.php: wp_ajax_generate_password()
  • wp-includes/class-wp-session-tokens.php: WP_Session_Tokens::create()
  • wp-admin/includes/network.php: network_step2()
  • wp-admin/install.php: display_setup_form()
  • wp-admin/includes/upgrade.php: wp_install()
  • wp-admin/includes/file.php: wp_tempnam()
  • wp-includes/pluggable.php: wp_new_user_notification()
  • wp-includes/pluggable.php: wp_salt()
  • wp-includes/user.php: register_new_user()
  • wp-includes/ms-functions.php: wpmu_activate_signup()
  • wp-includes/ms-deprecated.php: generate_random_password()
  • Show 8 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    Generate a 12 character password using these characters: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()

    
    <?php echo __( 'New password: ', 'textdomain' ) . wp_generate_password(); ?>
    
  2. Generate an 8 character password using only letters and numbers:

    
    <?php echo __( 'New password: ', 'textdomain' ) . wp_generate_password( 8, false ); ?>
    
    

    Generate an 10 character password consisting of letters, numbers, special characters (!@#$%^&*()), and extra special characters (-_ []{}<>~`+=,.;:/?|):

    
    <?php echo __( 'New password: ', 'textdomain' ) . wp_generate_password( 10, true, true ); ?>
    
    

    Generate an 10 character password consisting of letters, numbers, special characters (!@#$%^&*()), and extra special characters (-_ []{}~`+=,.;:/?|):

    
    <?php echo 'New password: ' . wp_generate_password( 10, true, true ); ?>
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文