以下是使用以下流线添加此功能的一种方法:
The admin updates the user option page:
-> edit_user_profile_update or personal_options_update hooks activated
-> edit_user() function is called
-> wp_update_user() function is called within edit_user()
-> wp_insert_user() function is called within wp_update_user()
-> profile_update hook activated within wp_insert_user()
for user updates, not user inserts
-> wp_redirect() function called on successful user updates
-> wp_redirect filter activated
-> The page reloads
-> admin_notices hook activated
步骤#1-HTML复选框:首先,我们在密码文本字段上方添加发送通知复选框,该复选框仅对管理员可见:
通过劫持show_password_fields
过滤器:
add_filter( \'show_password_fields\', \'wpse_notification_html\' );
function wpse_notification_html( $show )
{
if( current_user_can( \'manage_options\' ) ):
?>
<tr>
<th scope="row">
<label for="wpse_send_notification"><?php _e(\'Send a notification?\') ?></label>
</th>
<td>
<label for="wpse_send_notification">
<input type="checkbox" name="wpse_send_notification" id="wpse_send_notification" value="1" />
<?php _e( \'Send an email to user and notify that the password has changed.\' ); ?>
</label>
</td>
</tr>
<?php
endif;
return $show;
}
步骤2-主控制器:接下来,我们要连接到
profile_update
从“用户选项”页面:
add_action( \'edit_user_profile_update\', \'wpse_user_update\' );
add_action( \'personal_options_update\', \'wpse_user_update\' );
function wpse_user_update( $user_id )
{
if( current_user_can( \'manage_options\' ) )
add_action( \'profile_update\', \'wpse_controller\', 10, 2 );
}
其中主要逻辑在
wpse_controller()
功能:
function wpse_controller( $user_id, $old_user_data )
{
// Input:
$pass1 = filter_input( INPUT_POST, \'pass1\' );
$pass2 = filter_input( INPUT_POST, \'pass2\' );
$send = filter_input( INPUT_POST, \'wpse_send_notification\', FILTER_SANITIZE_NUMBER_INT );
// Run this action only once:
remove_action( current_action(), __FUNCTION__ );
// Send the notification:
if( 1 == $send )
{
if( ! empty( $pass1 )
&& $pass1 === $pass2
&& sanitize_text_field( $pass1 ) === $pass1
):
if( wpse_user_password_notification( $user_id, wp_unslash( sanitize_text_field( $pass1 ) ) ) )
add_filter( \'wp_redirect\', \'wpse_redirect_notification_success\' );
else
add_filter( \'wp_redirect\', \'wpse_redirect_notification_error\' );
else:
add_filter( \'wp_redirect\', \'wpse_redirect_pass_validation_error\' );
endif;
}
}
因为所有的验证都已经进行了。
显示错误/成功消息有点棘手,因为WordPress在更新用户选项页面时使用重定向。因此,我们对wp_redirect
筛选以添加相关查询变量,以便我们可以从admin_notices
挂钩:
function wpse_redirect_notification_success( $location )
{
return add_query_arg( \'wpse_notification\', \'mail_success\', $location );
}
function wpse_redirect_notification_error( $location )
{
return add_query_arg( \'wpse_notification\', \'mail_error\', $location );
}
function wpse_redirect_pass_validation_error( $location )
{
return add_query_arg( \'wpse_notification\', \'pass_validation_error\', $location );
}
步骤3-发送电子邮件:我们使用对核心的修改
wp_new_user_notification()
发送电子邮件的功能:
function wpse_user_password_notification( $user_id, $plaintext_pass = \'\' )
{
if ( empty( $plaintext_pass ) )
return false;
$user = get_userdata( $user_id );
$blogname = wp_specialchars_decode( get_option( \'blogname\' ), ENT_QUOTES );
$message = sprintf( __( \'Username: %s\' ), $user->user_login ) . "\\r\\n";
$message .= sprintf( __( \'New Password: %s\' ), $plaintext_pass ) . "\\r\\n";
$message .= wp_login_url() . "\\r\\n";
return wp_mail( $user->user_email, sprintf(__(\'[%s] Your username and new password\'), $blogname), $message );
}
步骤4-成功或错误的管理通知:我们使用以下管理通知,由
wp_notices
中添加的查询变量
wp_redirect
过滤器:
add_action( \'admin_notices\', \'wpse_admin_notices\' );
function wpse_admin_notices()
{
$status = filter_input( INPUT_GET, \'wpse_notification\', FILTER_SANITIZE_STRING );
switch ( $status )
{
case \'mail_success\':
?><div id="message" class="updated"><p><strong>Notification Sent!</strong>: Notification email successfully sent to the user</p></div><?php
break;
case \'mail_error\':
?><div class="error"><p><strong>ERROR</strong>: Notification email not sent to the user</p></div><?php
break;
case \'pass_validation_error\':
?><div class="error"><p><strong>ERROR</strong>: Notification email not sent to the user, because of symbol chars in the password </p></div><?php
break;
} // end switch
}
如何清理密码注意,我使用
sanitize_text_field()
清理电子邮件中的密码。我不知道最好的方法是什么。至少我不想在电子邮件中原始发送。这就是产生此额外错误消息的原因:
这样我们就可以处理密码中包含一些被sanitize_text_field()
作用欢迎任何其他想法。
附言:这是实验性的,可能需要一些调整。上面的代码都是过程性的,没有任何匿名函数。在上面涂抹一些OOP黄油可能会简单得多;-)