0

I have a plugin on my WordPress installation that allows users to suggest ideas. This results in them becoming the author of a post (although their role is still just "subscriber").

I received reports that these users were then getting the "New comment on your post" email that I (as admin) typically get for all other posts on the website (as I am the default author).

This isn't good as it shows the commentators email and IP. This should be reserved for admins only.

What ways are there to circumvent this? Either disallow these emails to non-admins, reroute all emails like this to the admin, or give non-admins a different email template?

enter image description here

1 Answer 1

0

I would probably approach this with a wp_mail filter to catch these messages before they are sent. That would be the simplest approach in my opinion.

You could create a filter function to screen email content and use key elements unique to these emails. My example will just target "New comment on your post" contained in the body, although you may want to fine tune and use additional text.

We'll also compare the email address the message is going to. I'm going to use the address stored in the db as the site's admin email. If you have a different address, you'll want to change this.

add_filter( 'wp_mail', 'my_wp_mail_filter' );
function my_wp_mail_filter( $args ) {

    // Get the site admin email.
    $admin_email = get_option( 'admin_email' );

    // What string are we looking for in the body?
    $needle = "New comment on your post";

    // Is this a new comment notification email?
    // Check for "New comment on your post" in body of message.
    if ( strpos( $args['message'], $needle ) ) {
        // If this is a new comment notification, who is it going to?
        // Check to see if the "to" address is not the admin.
        if ( $args['to'] != $admin_email ) {
            // This message is going to someone OTHER THAN the admin.
            // Return an empty array (dump all content, so email fails).
            return array();
        }
    }

    // Otherwise return unfiltered (so process can continue).
    return $args;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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