函数文档

wp_generate_user_request_key()

💡 云策文档标注

概述

wp_generate_user_request_key() 函数用于生成用户操作的确认密钥,并将哈希版本存储以备后续验证。它通过生成随机密码并更新相关请求的 post 数据来实现。

关键要点

  • 函数接受一个必需的整数参数 $request_id,表示请求 ID。
  • 返回一个字符串类型的确认密钥。
  • 内部使用 wp_generate_password() 生成 20 位随机密钥,并用 wp_fast_hash() 哈希后通过 wp_update_post() 存储到 post_password 字段。
  • 相关函数包括 wp_fast_hash()、wp_generate_password() 和 wp_update_post(),用于哈希、密码生成和 post 更新。
  • 被 wp_send_user_request() 函数调用,以发送确认请求邮件。
  • 自 WordPress 4.9.6 版本引入。

代码示例

function wp_generate_user_request_key( $request_id ) {
    // Generate something random for a confirmation key.
    $key = wp_generate_password( 20, false );

    // Save the key, hashed.
    wp_update_post(
        array(
            'ID'            => $request_id,
            'post_status'   => 'request-pending',
            'post_password' => wp_fast_hash( $key ),
        )
    );

    return $key;
}

📄 原文内容

Returns a confirmation key for a user action and stores the hashed version for future comparison.

Parameters

$request_idintrequired
Request ID.

Return

string Confirmation key.

Source

function wp_generate_user_request_key( $request_id ) {
	// Generate something random for a confirmation key.
	$key = wp_generate_password( 20, false );

	// Save the key, hashed.
	wp_update_post(
		array(
			'ID'            => $request_id,
			'post_status'   => 'request-pending',
			'post_password' => wp_fast_hash( $key ),
		)
	);

	return $key;
}

Changelog

Version Description
4.9.6 Introduced.