$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>comments - How to prevent usersauthors from seing IPemail of new commentators?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

comments - How to prevent usersauthors from seing IPemail of new commentators?

matteradmin9PV0评论

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?

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?

Share Improve this question edited Jan 15, 2019 at 3:01 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Jan 10, 2019 at 22:34 yesbutmaybenoyesbutmaybeno 1655 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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;
}
Post a comment

comment list (0)

  1. No comments so far