我对WordPress编程非常陌生,我甚至不确定这个功能是否存在。我想做的是在管理员更改密码/电子邮件时向用户(而不是管理员)发送电子邮件?
目前,当我添加新用户时,会向他们发送电子邮件,但当我更改他们的密码/电子邮件时,不会向他们发送电子邮件。
我将非常感谢您的帮助。
我对WordPress编程非常陌生,我甚至不确定这个功能是否存在。我想做的是在管理员更改密码/电子邮件时向用户(而不是管理员)发送电子邮件?
目前,当我添加新用户时,会向他们发送电子邮件,但当我更改他们的密码/电子邮件时,不会向他们发送电子邮件。
我将非常感谢您的帮助。
以下是使用以下流线添加此功能的一种方法:
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黄油可能会简单得多;-)
有几种方法可以实现这一点;
1) [简单]将用户发送到登录屏幕,让他们启动忘记密码过程
2) [中等]插件(有一些)免费WordPress插件,this looks useful, 或者你可以搜索http://wordpress.org/plugins/search.php?q=reset+password 可能还有很多商业广告,but here is one
3) [硬]自定义在您自己的插件中编写函数-为此,您可以参考WordPress Codex 还有here seems a reasonable tutorial
自WordPress 4.3.0以来,有一个用于此的过滤器(位于user.php
):
add_filter(\'send_password_change_email\', function( $send, $user, $userdata ){
// bail early if $send is false
if( !$send ) {
return $send;
}
// Your custom email logic
// ...
// tell wordpress not to send the built-in email
return false;
}, 10, 3);
我在使用WP_User_Query 要按元数值对用户进行排序,我认为这很简单,因为它只按字母降序显示结果。 <?php $args = array( \'orderby\' => \'meta_value_num\', \'meta_key \' => \'epicredvote\', \'order\' => \'DESC\', ); // The Query $user_query = n