I'm trying to add a link to each item of the Users table in the WP Admin area and use that link to send that specific user an email. I'm most of the way there but I'm having some odd behavior that I can't figure out. Here's my code: (I describe my issues below that)
// Adds "Send rejection email" action to Users page
function jm_send_rejection_link($actions, $user_object) {
if( ($_POST['send']) && ($_POST['email-address'] == $user_object->user_email) ) {
$sendto = $_POST['email-address'];
$sendsub = "Your registration has been rejected.";
$sendmess = "Sorry! Your registration has been rejected. I guess someone doesn't like you.";
$headers = array('From: The Company <[email protected]>');
wp_mail($sendto, $sendsub, $sendmess, $headers);
echo '<div class="updated notice"><p>Success! The rejection email has been sent to ' . $_POST['email-address'] . '.</p></div>';
}
$actions['send_rejection'] = "<form method='post' action=''>
<input type='hidden' name='email-address' value='" . $user_object->user_email . "' />
<input type='submit' name='send' value='Send Rejection' />
</form>";
return $actions;
}
add_filter('user_row_actions', 'jm_send_rejection_link', 10, 2);
So this code adds the action link "Send Rejection Email" to each user in the table. It also successfully sends out the email... sometimes. For the first user in the table, it seems to fail and also it appends some unexpected stuff to the URL:
http://sampledomain.org/wp-admin/users.php?s&action=-1&new_role&paged=1&email-address=emailaddress%sampledomain.com&send=Send+Rejection&action2=-1&new_role2
It also does not display the success message in this case.
For a number of the other users it does successfully display the success message and send the email. However, not for all other users. The other users display the success message but do not receive the email. It is possible that these accounts are just catching the email in a firewall or something, although it does not appear in spam.
I am most concerned about whatever I'm doing wrong that is causing the issue with the first user. Any ideas?
PS - I do recognize that my IF statement in there is a little weird. I guess because I'm adding this to user_row_actions
it is running for every user on the table, which is why I am making that email send only for the user whose email matches the posted address. A little backwards I know. If I should break those into two different functions, any advice as to how to make that second function run on the user page without running for each user would be much appreciated.