wp_authenticate
云策文档标注
概述
wp_authenticate 是一个在用户认证前触发的动作钩子,位于 wp_signon() 函数内部,允许通过引用修改用户名和密码参数。它与 wp_authenticate() 可插拔函数不同,专注于认证前的自定义处理。
关键要点
- 触发时机:在 WordPress 认证过程之前执行,区别于 wp_login 动作。
- 参数传递:$user_login 和 $user_password 通过引用传递,回调函数可修改这些值。
- 位置:位于 wp_signon() 函数中,用于实现自定义登录机制或认证逻辑。
- 注意事项:不要与 wp_authenticate() 可插拔函数混淆,后者是用于实际认证的函数。
代码示例
function wpdocs_my_function( &$user_login, &$user_password ) {
$user_login = 'new_username';
$user_password = 'new_password';
}
add_action( 'wp_authenticate', 'wpdocs_my_function', 10, 2 );注意事项
- 回调函数声明时必须使用 & 符号来接收引用参数,以允许修改值。
- 此钩子可用于实现基于角色、电子邮件或其他自定义条件的认证流程。
原文内容
Fires before the user is authenticated.
Description
The variables passed to the callbacks are passed by reference, and can be modified by callback functions.
Parameters
$user_loginstring-
Username (passed by reference).
$user_passwordstring-
User password (passed by reference).
Source
do_action_ref_array( 'wp_authenticate', array( &$credentials['user_login'], &$credentials['user_password'] ) );
Changelog
| Version | Description |
|---|---|
| 1.5.1 | Introduced. |
Skip to note 4 content
Chad Cloman
To actually modify the values, it’s necessary to use ampersands in the callback function declaration:
function wpdocs_my_function( &$user_login, &$user_password ) { $user_login = 'new_username'; $user_password = 'new_password'; } add_action( 'wp_authenticate', 'wpdocs_my_function', 10, 2 );Skip to note 5 content
Steven Lin
Example migrated from Codex:
You can use wp_authenticate action hook to implement a custom login mechanism before you involve WordPress.
prefix . 'capabilities'; $caps = $userinfo->$property; foreach ( $caps as $role ) { if ( 'special_authenticator' == $role ) { wpExternalLoginProcess( $username, $_POST['pwd'] ); } } } ?>Skip to note 6 content
Steven Lin
Example migrated from Codex:
You can also use email to authenticate users in WordPress.
user_login; } } add_action( 'wp_authenticate', 'wp_authenticate_by_email' ); ?>