你的方法有缺陷。
您正在使用的过滤器挂钩-user_row_actions
- 用于向用户行添加“悬停链接”。它在用户>所有用户屏幕中的每个用户行上运行。如果您使用过滤器只是添加链接,那么该部分本身就可以了但是,您正在使用过滤器来处理发送电子邮件的操作。
该过滤器在用户>所有用户(表视图中显示的默认用户数)的页面加载上运行20次,因此当您这样做时,最终会发送20封电子邮件(或视图中的用户数)。
所以这里有一条经验法则——过滤器是用来改变显示方式的。操作用于运行某种类型的进程。So use the filter to add the link, and an action to process the result if the link is clicked.
继续使用user_row_actions
筛选以添加链接。我建议使用动作钩,如admin_notices
检查您的链接是否已提交,然后对其进行处理,因为您无论如何都需要使用该挂钩来显示“成功”消息。这样,您就可以将这两个过程结合起来。这样,您对单击的链接的处理将只运行一次,而不是20次。
这是按照我上面所述修改的原始代码。我已经加入了一些评论来澄清它在做什么以及为什么。
// Adds "Send verification email" action to Users page
add_filter(\'user_row_actions\', \'send_verification_email_link\', 10, 2);
function send_verification_email_link($actions, $user_object) {
$actions[\'send_rejection\'] = "<a class=\'send_verification_email\' href=\'" . admin_url( "users.php?action=send_verification_email&user_id=" . $user_object->ID ) . "\'>" . __( \'Send Verification Email\' ) . "</a>";
return $actions;
}
add_action( \'admin_notices\', \'send_verification_email\' );
function send_verification_email() {
// Get the current screen so you only move forward if this is the users.php screen.
$screen = get_current_screen();
if ( \'users\' == $screen->id ) {
// Make sure to check if the $_GET value is actually set before you compare the value,
// otherwise you may get errors if the page is loaded without it.
if ( isset( $_GET[\'action\'] ) && \'send_verification_email\' == $_GET[\'action\'] ) {
// Check that the email was passed too.
$user_id = ( isset( $_GET[\'user_id\'] ) ) ? $_GET[\'user_id\'] : false;
// Get the user info object info using the user ID (you\'ll need
// this for the email address and other data like "first_name" you use in the email message.
$user_object = get_user_by( \'ID\', $user_id );
// If you have a user with an email address.
if ( $user_object->user_email ) {
$my_logo = "<img src=\'https://example.com/\' alt=\'logo\' />";
$subject = \'Your Example Account is not yet verified\';
$headers = array(\'Content-Type: text/html; charset=UTF-8\');
$message = \'<p style="text-align: center;">\'.$my_logo.\'</p>
<div style="max-width: 600px; margin: 30px auto; padding: 15px; font-family: Roboto,Open Sans,Helvetica,Arial; border: 1px solid #e8e8e8; line-height: 1.6; text-align: center; background-color: white;">
<div style="text-align: left;">
<p>Hi \'.$user_object->first_name.\',</p>
<p>Thank you for registering your business on Example.com. Your account is not yet verified because you have not provided the verification information or the information you provided is invalid. Please provide the necessary information below to get verified:</p>
<p><strong>Private person:</strong></p>
<ul>
<li>Upload a profile photo.</li>
<li>Upload an identification document.</li>
</ul>
<p><strong>Company:</strong></p>
<ul>
<li>Upload a profile photo.</li>
<li>Upload an identification document.</li>
<li>Provide your registered company name.</li>
<li>Provide your company registration number.</li>
</ul>
<a style="text-align: center;" href="https://example.com/login/" rel="noopener noreferrer" target="_blank">Click here to login to your account.</a>
<p>When you are logged in, click the settings icon on your profile page and click edit profile. Scroll to the verification section to provide the verification information.</p>
<p>Please let us know if you have any questions.</p>
<p>Kind regards,<br>Example Team</p>
</div>
</div>\';
// Send message.
wp_mail( $user_object->user_email, $subject, $message, $headers );
// Set up admin notice
$class = \'updated notice\';
$admin_notice = \'Success! The verification email has been sent to \' . $user_object->user_email;
} else {
// Set up admin notice for error state.
$class = \'notice notice-error\';
$admin_notice = "No email address was passed so no email was sent.";
}
// Display message.
printf( \'<div class="%s"><p>%s</p></div>\', $class, $admin_notice );
}
}
}