if someone mention “Sharkboy”, what is the result you like?
Nothing if it’s in the comment itself. I only want to disallow it in the “name” field.
I am using open comments, so people don’t have to register. People just fill in their name, but it shouldn’t be “Sharkboy” because then people will think it’s the real author.
I can’t be the first person to want this, right? 😀
I’m now thinking of editing the WordPress code to manually check submitted names if they contain our author’s names, but I’m afraid I will lose my changes when I update WordPress.
I really wish there was a plug-in for this, or a bit more advanced settings in the “Discussion settings”.
Hello, wobblypop, according to what you say, I think there are two solutions. Either you refuse the commentators which means their comment won’t be submitted, either their name will be changed, for instance, to anonymous automatically when the comment is submitted if the name filed contains “Sharkboy”.
As for the second one, it can be done if we could find the right filter. Do you like the second solution?
Hi Hedonplay, thanks for taking the time to answer.
I like the second idea best, just automatically changing the name to “Anonymous”. Do you think a plugin is already out there? I searched but couldn’t find one.
I don’t mind coding one myself, but I don’t want to reinvent the wheel ofcourse!
/*
**change the comment author name if it contains any string that are disallowed, when saving a new comment in the database
*/
if(is_author()){ //do not apply the filter if the author post a comment
add_filter( 'preprocess_comment' , 'wh_change_comment_author' , '99' );
}
function wh_change_comment_author($commentdata)
{
$name_disallowed='sharkboy'; //you can change it as you need
$name_default='anonymous';
//the comment_author is contained in the array $commentdata
if(stristr($commentdata['comment_author'],$name_disallowed)) {
$commentdata['comment_author']=$name_default; //replace the name disallowed by the default one
}
return $commentdata;
}
add this piece of code to the function.php which is in your theme directory.
Hopefully it works.
Hi Hedonplay,
Thank you very much! I only changed the code slightly to get it to work (I moved the “is_author” check inside the function). This works perfectly! Thanks for the help, it’s much appreciated.
/*
**change the comment author name if it contains any string that are disallowed, when saving a new comment in the database
*/
function wh_change_comment_author($commentdata)
{
if(!is_author())
{
$name_disallowed='sharkboy'; //you can change it as you need
$name_default='anonymous';
//the comment_author is contained in the array $commentdata
if(stristr($commentdata['comment_author'],$name_disallowed)) {
$commentdata['comment_author']=$name_default; //replace the name disallowed by the default one
}
return $commentdata;
}
}
add_filter( 'preprocess_comment' , 'wh_change_comment_author' , '99' );