0

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@sample.com>');
           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.

  • the problem is that the entire table is already inside a "form" then you cannot nest another form inside. if you want to send the e-mail with one click you can try to make a link in the actions. if it can help to send to multiple users, try to add a bulk action. – mmm Jul 11 '17 at 22:24
0

Thanks for that guidance, mmm. That was helpful. I resolved the problem this way:

// Adds "Send rejection email" action to Users page
function jm_send_rejection_link($actions, $user_object) {
     if( ($_GET['action'] == 'jm_send_rejection') && ($_GET['user'] == $user_object->user_email) ) {
           $sendto = $_GET['user'];
           $sendsub = "Your registration has been rejected.";
           $sendmess = "Your registration for has been rejected.";
           $headers = array('From: The Company <email@domain.com>');
           wp_mail($sendto, $sendsub, $sendmess, $headers);
       echo '<div class="updated notice"><p>Success! The rejection email has been sent to ' . $_GET['user'] . '.</p></div>';
          }

    $actions['send_rejection'] = "<a class='jm_send_rejection' href='" . admin_url( "users.php?action=jm_send_rejection&amp;user=" . $user_object->user_email ) . "'>" . __( 'Send Rejection' ) . "</a>";
    return $actions;
}
add_filter('user_row_actions', 'jm_send_rejection_link', 10, 2);

This definitely corrected the issue I was having with the first user in the list. It did not resolve the issue with the one other account not receiving emails, but I am 90% sure this has something to do with that account's email server settings, rather than with my code.

0

The Issue about notice is that you are not using admin notice function

function jm_send_rejection_link($actions, $user_object) {
        if( ($_GET['action'] == 'jm_send_rejection') && ($_GET['user'] == $user_object->user_email) ) {
        $sendto = $_GET['user'];
        $sendsub = "Your registration has been rejected.";
        $sendmess = "Your registration for has been rejected.";
        $headers = array('From: The Company <email@domain.com>');
        wp_mail($sendto, $sendsub, $sendmess, $headers);
    add_action ('admin_notice', function(){
?>
<div class="updated notice"><p>Success! The rejection email has been sent to <?php echo $_GET['user']; ?> </p></div>
<?php
});
        }

  $actions['send_rejection'] = "<a class='jm_send_rejection' href='" . admin_url( "users.php?action=jm_send_rejection&amp;user=" . $user_object->user_email ) . "'>" . __( 'Send Rejection' ) . "</a>";`
  return $actions;
}
 add_filter('user_row_actions', 'jm_send_rejection_link', 10, 2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.