Restrict comments with regular expression
I want to prevent users to write email addresses in comments.
I know we can restrict words in wp-admin Settings Discussion Disallowed Comment Keys.
but can we add a RegExp such ([a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)
to restrict all the emails?
I was trying to add something like:
//Regular expression to ban emails
function te_check_comment($approved, $commentdata){
if(preg_match('#\b((?=[a-zA-Z0-9+._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+){1,5}\b#i',$commentdata[comment_content])){
$approved = 0;
}
return $approved;
}
add_action('pre_comment_approved', 'te_check_comment', 10, 2);
Source:https://techearl.com/wordpress/wordpress-moderate-comments-using-regular-expressions