Force Valid Email Addresses

In WordPress, you can enable an option that requires commenters to supply a name and an email address, but oddly enough, it doesn’t validate the email address submitted. This is simple enough to add, as WordPress already has a function for validating email addresses, and because it can be done via a REALLY simple plugin.

Just save this plugin in your (enabled) my-hacks.php file or as its own file (your choice… it all depends on how cluttered your plugin screen is getting).

<?php
/*
Plugin Name: Force Valid Email in Comments
Version: 1.0
Plugin URI:
Description: This simple function is run when comments are being submitted, and uses a built-in WordPress function to ensure that commenters’ e-mail addresses are at lead validly formed. It only does this check if you have WordPress set to require name and e-mail.
Author: Mark Jaquith
Author URI: http://www.txfx.net/
*/
function force_valid_comment_email($comment){
global $email;
if ( get_settings(’require_name_email’) && !is_email($email))
die( __(’The email address you entered was invalid.’) );
return $comment;
}
add_action(’post_comment_text’, ‘force_valid_comment_email’, 1);
?>

One thing to note: it only performs the check if you have WordPress set to require name and email. If you don’t require an email address, what do you care if it is validated? Then again, if you do want it to check regardless, just change the conditional statement to this:

if (!is_email($email))

13 Responses to “Force Valid Email Addresses”

  1. Mama Write Says:
    Various WordPress Links
    Rather than ping ScriptyGoddess multiple times and clog my sideblog with even more WordPress links, I decided to plop ‘em all here. I’m going to give WordPress a try again on my course website, since there are various hacks that…

  2. star shaped » New WP plugins on star shaped Says:
    [...] Auto shutoff comments, which disables commenting on entries after an x amount of days, and Force valid email addresses, which only accepts emails in the typi [...]

  3. Josh Says:

    Hi there, I’m just a random passer by, but I had a couple questions, but not to be a pest.

    First off, I like this hack, however, i dont have a my-hacks.php anywhere and have been unable to figure out where that file should go and what else it should contain.

    Also, 2 features of your blog that I really like and was wondering if you could point me to the right place to add them to my blog.

    One would be the “Read more…” and “Read more here…” Thats a really nice feature, is there a site explaining how to install that? or is it a custom hack?

    Second, I really like the buttons above this comment box, taking away those yuckie looking tags that are normally up there, and putting those buttons would be really nice on my blog. Is there a page for that hack? or again is it custom?

    Thanks for your time.
    Josh.

  4. Jennifer Says:

    Josh - Updated the post to point to the wiki page about the my-hacks.php page - it’s here:

    Read more.. thing is the show/hide plugin here.

    The buttons are here.

    Just about everything you see on this blog is published or bookmarked on the site. So just use the search form at the top and you should be able to find it.

  5. Josh Says:

    Very cool, thanks for the quick reply. Looks like there is another cool hack i like, that img that identifies you as the blog author and me as subscribed on this comments page.

    Thanks for the help.

  6. Blue Lamp » New plugins Says:
    [...] ing Aubrey’s lead, I installed two new plugins today. One requires you to enter a valid email address (something@s [...]

  7. bookmarks » scriptygoddess Says:
    [...] « | Home 8/28/2004

    scriptygoddess: force valid email addresses for WP commenters [...]

  8. julian Says:

    how about a plugin that checks if an e-mail is active as in it’s not anything@anyaddress.com that’s just made up by the poster?

  9. Dianne Says:

    Sigh. Having to set all this stuff up again due to server problems and for the life of me I can’t get this to work, as witnessed by the 9 or so bogus comments I made on my latest post! LOL. Any suggestions? (www.rigdonia.com/wp)

  10. Dianne Says:

    erm. Nevermind. ;-)

  11. Ron Says:

    This has decided not to work when a user is logged in and tries to make a comment. Is there any way to get this to work when calling from a users profile?

  12. Ron Says:

    Is there any way to make an exclusion to this rule if a user is logged in?

  13. WordPress Hack: Force Valid Email Addresses | IDGLabs.COM Says:

    [...] hack uses an existing function built into WP to validate eMail addresses of those placing comments to [...]