Last active
April 28, 2023 08:37
-
-
Save mattiasghodsian/8ac5c2f7578c551227e586ce39608d91 to your computer and use it in GitHub Desktop.
[Wordpress] Filter bad words in comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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