Welcome to Dagon Design. In addition to free scripts, WordPress plugins, and articles, we offer a variety of services including custom theme design, plugin creation, and PHP scripting. Contact me for more information.

Updated Friday, February 20th, 2009 at 8:20pm

Prevent author impersonation in WordPress comments

This modification to WordPress prevents unregistered comment authors from using the names or email addresses of the registered authors on your site. It does this by first checking to see if the comment author is logged in. If they are not, it compares their name and email address to the registered author data. If there is a match, the comment is blocked and a custom message is displayed. The name and email address comparison is case-insensitive.

Requirements

This code modification has been tested in WordPress 2.2 through 2.8+

Instructions

1) Open /wp-comments-post.php for editing (backup the file first!)

2) Find the following block of code:

Notice: In WordPress 2.8, the code has changed a bit, but should be easy to find near the top of the page.

$comment_author       = trim(strip_tags($_POST['author']));
$comment_author_email = trim($_POST['email']);
$comment_author_url   = trim($_POST['url']);
$comment_content      = trim($_POST['comment']);

3) After it, add the following:

// get list of user (display) names for blog
global $wpdb;
$valid_users = (array)$wpdb->get_results("
  SELECT display_name, user_email FROM " . $wpdb->prefix . "users");

// get ID of logged in user (if there is one)
global $userdata;
get_currentuserinfo();
$logged_in_name = $userdata->ID;
$logged_in_email = $userdata->user_email;
 
// see if the comment author matches an existing author
$found_match = FALSE;
foreach ($valid_users as $va) {
  if (trim($va->display_name) != '') {
    if (strtolower($va->display_name) == strtolower($comment_author)) {
      $found_match = TRUE;
      break;
    }
  }
  if (trim($va->user_email) != '') {
    if (strtolower($va->user_email) == strtolower($comment_author_email)) {
      $found_match = TRUE;
      break;
    }
  }  
}

// if commenter is not logged in, but match was found, block the comment
if (trim($logged_in_name) == '') {
  if ($found_match == TRUE) {
    wp_die( __('You cannot post using the name or email of a registered author.') );
  }
}

4) Save and close the file

Notes

To test this modification, simply log out and try to post a comment using the name that displays when you regularly post comments (when you are logged in).

If you would like to change the message, just modify this line:

wp_die( __('You cannot post using the name or email of a registered author.') );

  If you have found this page useful, please consider donating. Thanks!

  1. I highly recommend Dagon Design’s Multi Column links. Check out how it looks on my site.LV

  2. it’s easy plugin to use and very useful for people who use wordpress as their platform. bravo for dagon design COACH

  3. I have been very optimistic about you, I think you can certainly make some big business. Just do you good. Believe me. Handbags

  4. It is well known that unity is strength, a chopstick is very fragile, ten chopsticks hard reality, not so fast can be broken. So, no matter how many contradictions, we should be united together, is not it, Oh, I hope to peaceful coexistence. Handbags

  5. Coach is a leading American designer fydf

  6. Coach is a leading American designer

  7. him realize that its his calling to reach out to them and share his skills and knowledge to turn them into better persons and athletes.European Championship qualifying, group A has a competition at home.

  8. i liked it .thanks for shairing.

  9. Hey, very intrusting and informative Video .it is realy useful of my side . i liked it .thanks for shairing.

  10. I usually find great deals on ebay and with ma

  11. bottleless water coolers I will watch this on youtube later when I am done with my work even it has low video quality

  12. Hey, very intrusting and informative Video .it is realy useful of my side . i liked it .thanks for shairing.

  13. wished to write in my site something like that.

  14. Brand Scientific research cen

  15. he COACH handbag receives people’

  16. Thank you so very much for taking

  17. ?the pier at Gulfport precisely where riggings of shrimp boats are loose stitches

  18. tell me .you like it so much??

  19. Burberry Handbags are one of best selling brand Bags around the world. Burberry

  20. Our online shop offering the most popular Gucci Men’s Sandals, We deal with world brand Gucci Thong Sandals For Men. You will find them are the most stylish and up-to-date. Safe and fast shipping is worth to have a try.

  21. Thanks for a nice share you have given to us with such an large collection of information dvvfffd

  22. I wouldnt have ever noticed you if I never tried look and research. Superb and excellent and aim delighted I saw it. Now I know what I need to do. Thanks ever so much.

  23. Nice hosting! It’s my first time to read like this article. hndhnf

  24. vgs This is a wonderful site! I’ve been looking for something like this 
    for a while now! Thank you!
    

  25. it’s very useful.rfg

  26. This webpage is just what i was looking for – thank you for sharing all this.

  27. Thanks for a nice share you have given to us with such an large collection of information dvvfffd

  28. Thank you so very much for taking the time to share…very useful, indeed!cvbf

  29. Thanks for a nice share you have given to us with such an large collection of information dxxffd

  30. 59
    shaki

    i appreciate this effort,..
    best regards,..

  31. Thanks for all the clarification and help. It was really very supportive.

  32. Thanks a lot for the help and advice.

  33. Thx for the comments!

  34. I visited this page first time and found it Very Good Job of acknowledgment and a marvelous source of info………Thanks Admin!

  35. The word press has really help a lot. Keep it up.

  36. I want to thank you for the efforts you write in this post. I hope the same best work from you in the future. Thanks for sharing us a great information that is actually helpful. Good day! bachelor in fire science

  37. A very very funny series thank you one and all for bringing it to our screens.

  38. I want to be a part of this mags. I have idea to make a good design and to make a good layout for this mags. We need to make other people to feel interesting by see our mags cover, then they will buy it criminal justice bachelors

  39. 50
    sun

    My 2 year old son has been in the hospital for bilateral pleural chylous effusions since Jan 29, p/o Fontan in November of 2009. He has tried diet, diuretics, thoracic duct ligation and now pleurodesis on the right side. end of tenancy cleaning

  40. 49
    sun

    My 2 year old son has been in the hospital for bilateral pleural chylous effusions since Jan 29, p/o Fontan in November of 2009. He has tried diet, diuretics, thoracic duct ligation and now pleurodesis on the right side. Donnie Boddy

  41. 48
    sun

    please make sure you have read the entire article. Also, please read through the comments because most common issues have already been discussed many times. Thanks. healthcare IT degree

  42. please read through the comments because most common issues have already been discussed many times. Thanks. information technology masters degree

  43. nice tip. it’s work

  44. Wow , I was looking for this tips until i found this
    It’s very nice tips.masters degree sports management

  45. yes agree with people. This is the nice tip we should know. Go Go

  46. please read through the comments because most common issues have already been discussed many times. Thanks.healthcare administration degree online

  47. Thanks so much! Great plugin.
    mp4 to dvd, itunes to dvd

  48. Thank you thank this ia a nice tip

  49. 40
    person_guyz

    Best-of-Programm jedoch probiert Josef Hader etwas, was viele Kollegen machen, er aber normalerweise nicht: Nummern spielen und dazwischen Lieder singen.online engineering masters degrees

  50. 39
    jibran

    Companies these days are using ergonomic furniture to ensure that there are no injuries from work related hazards. Use an ergonomic chair and feel better comfort in your back and shoulder. Bed Bugs

  51. 38
    webseo67

    Hi! I tried to use your plugin but it shows me the last XX posts instead of the posts of the last XX days…
    mary kay skin care

  52. 37
    jibran

    Best-of-Programm jedoch probiert Josef Hader etwas, was viele Kollegen machen, er aber normalerweise nicht: Nummern spielen und dazwischen Lieder singen. best movie download sites

  53. Wow , I don’t know about this tips before.
    I do like Prevent author impersonation in WordPress comments as you post in tutorial.

  54. And if you download the right software, you can MP4 to DVD. How to remove DRM protection from M4V, WMV, M4P, AAC, M4A, WMA, OGG, iTunes, Napster, Rhapsody legally? So that you can burn iTunes to DVD or convert DRM files to other unprotected formats.

  55. Wow , I was looking for this tips until i found this
    It’s very nice tips.

  56. Thank. This ia a nice tip!

  57. it’s help a lot to me

  58. oww nice . thank you .)

  59. 30
    John

    I also need this for WP 3.0. Has anyone got it working on the new version yet?

  60. Hi, it seems like this doesn’t work on WP 3.0. Do you know what modifications should be made to make it work? Thank you.

  61. We supply all kinds of moncler boots,moncler bags,moncler coats,moncler jackets,moncler T-shirt,moncler vest and so on.We have got a good reputation pf our products with top quality and good price.We sincerely look forward to futher cooperation with you for mutual benefits.We are sure that you will find working with us is a pleasant, time-saving and profitable experience. Please feel free to contact us if you have any questions.Our website is http://www.newlyapparel.com.

  62. @Jake
    Might be a little late now but if you remove all references to name like “if (trim($logged_in_name) == ”) {” it should only check email.

  63. 26
    wpbloggy

    A VERY USEFUL bit of code! Thank you for sharing this useful technique, I’ve wanted such a feature for some time.

  64. 25
    Nothing

    i just test its working on Version 2.9.2 , great thing thanks alot :D

  65. 24
    Chip D

    Has anyone tried this in WP 2.9.1 yet?

  66. I really like how this works, however I would like it to ONLY check the email. I don’t care if someone uses the same name, but if they use the same email it will use their gravatar. I get way too many comments and have already seen many users with the same names.

    Could you post a version that only checks email?

    Thank you, I appreciate it and this works great even on 2.9.1

  67. WOW!!!
    This is absolutely amazing (and if left open, dangerous!)

    I can’t believe the boffins who wrote WP didn’t think about something like this and implement a fix!

    I have started to update all my (38) blogs… muchos grassears dude!

  68. I love this – thanks so much for it. Is there a way to edit the output so that if someone wrote a long comment, that their text won’t be lost?

    For example, someone could have no intentions of impersonating anyone, write a very good and long comment using the name “Mike”, and understandably not know that it was already registered. How can they be notified that their comment won’t be submitted until they change their name, but not lose what they wrote?

    I imagine it would involve notifying them without leaving the page they’re on.

  69. There are certainly a number of biological differences between men and women. ,

  70. Where all content is aggregated for you, ready to be consumed. ,

  71. tancks.
    This Post Helped me
    Good Time

  72. Fantastic, thank you for sharing this. It works like a champ. Now websites cannot be stolen from people who post a lot.

  73. that is bloody cool, i love that tip.

  74. I have written a small plugin, so one doesn’t have to change any core files.

  75. This has been tested, and works, in the latest release of WordPress – 2.8

    (The code you look for has changed a bit, but should be easy to find near the top of the file)

  76. Is this working for wp 2.8 ? I am gonna check and will update here if it does not. I believe it should

    Thanks

  77. great plugins, that one must have. Appreciate them

  78. Great for OEM author’s. Nice i also try this. Thanks for sharing with us.

  79. nice hack/code.

    how would i add a back button to the error message to help commenters try to comment again. as is, just brings up an error page with the message.

    also, is it possible to show the message without revealing the location of the wp_comments_post file?

    thanks

  80. 9
    ListenUp

    Fantastic. Thanks. Was having trouble with some funny guys at my site using admin (that’s me) to reply to other people visiting the site. Best part is it doesn’t allow variants of the name say admin such as Admin or AdMin etc.
    Super stuff. Thanks. Works for 2.7 Wordpress by the way.

  81. This code has been tested in the latest release of WordPress (2.7.1)

  82. 7
    Brokakeroko

    I like your site. Brokakeroko

  83. 6
    Ipstenu

    I heavily cribbed from both you and Marco Luthe to make this plugin. It seems to be working right now, though I’m sure someone could hack it:

    function wp_prevent_imposters( $commentdata){
    
    // get list of user (display) names for blog
    global $wpdb;
    $valid_users = (array)$wpdb->get_results(" SELECT display_name, user_email FROM " . $wpdb->prefix . "users");
    
    global $userdata;
    get_currentuserinfo();
    
    // get email of current user
    $logged_in_email =  $commentdata['comment_author_email'];
    $logged_in_name  =  $commentdata['comment_author'];
    
    // see if the comment author matches an existing author
    $found_match = FALSE;
    foreach ($valid_users as $va) {
      if (trim($va->display_name) != '') {
        if (strtolower($va->display_name) == strtolower($logged_in_name)) {
          $found_match = TRUE;
          break;
        }
      }
      if (trim($va->user_email) != '') {
        if (strtolower($va->user_email) == strtolower($logged_in_email)) {
          $found_match = TRUE;
          break;
        }
      }
    }
    
    // if commenter is not logged in, but match was found, block the comment
      if ($found_match == TRUE) {
        wp_die( __('You cannot post using the name or email of a registered author.') );
      }
      else {
        return $commentdata;
      }
    
    }
    
    add_filter('preprocess_comment', 'wp_prevent_imposters');
    

  84. Thanks for the inspiration! I have written a small plugin, so one doesn’t have to change any core files.

    http://www.saphod.net/2008/10/14/how-to-prevent-commenters-from-using-your-email/

  85. Tracey: That is a good idea. I just made a modification to the code.

    I also tested to see if this modification will work in WordPress 2.5, and indeed it does. :)

  86. 3
    Tracey

    How would you tweak this to prevent someone inserting a registered user’s email address instead of their own?

    For example, my site uses gravatars but a user could ‘impersonate’ someone else if they know that person’s email, at which point the gravatar will display for a user who may not have made the comment.

    Hope this makes sense!

  87. Nice hack, great explanation!

  88. thanks

Leave a Comment

Before you comment: If you are having an issue with a script, please make sure you have read the entire article. Also, please read through the comments because most common issues have already been discussed many times. Thanks.


Be sure to wrap all code in <code></code> tags.