02 Jul, 2004
Force Valid Email Addresses
Posted by: Jennifer In: WordPress my-hacks additions
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).
Plugin Name: Force Valid Email in Comments
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/
if ( get_settings('require_name_email') && !is_email($email))
die( __('The email address you entered was invalid.') );
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: