Skip to content

Instantly share code, notes, and snippets.

@mattiasghodsian
Last active April 28, 2023 08:37
Show Gist options
  • Select an option

  • Save mattiasghodsian/8ac5c2f7578c551227e586ce39608d91 to your computer and use it in GitHub Desktop.

Select an option

Save mattiasghodsian/8ac5c2f7578c551227e586ce39608d91 to your computer and use it in GitHub Desktop.
[Wordpress] Filter bad words in comments
/**
* Title: Filter bad words in comments
* Author: Mattias Ghodsian
* Description: Filter bad words in comments
* Donate a cup of coffee: https://www.buymeacoffee.com/mattiasghodsian
* Donate Eth: 0xBBB96204E45D11C9799c6B12E6eE6F0d4A071Ef5
**/
function filter_text( $comment_text, $comment = null )
{
$words = ['grym', 'great', 'awesome', 'good'];
$words = array_map('preg_quote', $words);
$comment_text = preg_replace_callback('~(?:'.implode('|',$words).')~i', function($matches){
return substr($matches[0], 0, 1).str_repeat('*', (strlen($matches[0]) -2) ).substr($matches[0], -1);
}, $comment_text);
return $comment_text;
}
add_filter( 'comment_text', 'filter_text', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment