I am developing a swear filter plugin for my blog. It is supposed to deny submissions containing any words in a text file called swears.txt. Here is the code I am using for the comment form on single post page:

if (isset($_POST['submit'])) {
    echo 'Submitted'; // for debugging purposes
    $swearsFile = 'swears.txt';
    $swearsCntnt = file_get_contents($swearsFile);
    $swearList = explode("\n", $swearsCntnt);
    $swearWords = "/";
    for ($i = 0; $i < count($swearList); ++$i) {
        if ($i == 0)
            $swearWords .= rtrim($swearList[$i]);
            $swearWords .= "|" . rtrim($swearList[$i]);
    $swearWords .= "/i";
    if (preg_match($swearWords, $_POST['comment'])) {
        wp_die( __( '<strong>ERROR</strong>: You have entered forbidden text. Please consider revising.' ), 200 );

The problem is that nothing happens after I press the Post Comment button except that the submitted comment appears as a new one. I am using an echo to verify that the Post Comment button has been pressed whether or not its following statements return true, yet nothing happens. How do I get WordPress to acknowledge if (isset($_POST['submit']))?

3 Answers 3


This might not directly answer your question (because I haven't even thought about why the $_POST['submit'] would be ignored or if it is even correct), but you should use filters/actions whenever possible.

The correct filter for filtering comment text is pre_comment_content, and you should use it like this:

function my_filter_comment_for_swear_words( $comment_content ) {
    // Verify if $comment_content contains swear words here
    // return $comment_content if it doesn't contain forbidden words
    // or wp_die if it does
add_filter( 'pre_comment_content', 'my_filter_comment_for_swear_words' );

This code would, as usual, go into your functions.php file or into your custom plugin (depending on what your setup is).

  • It looks like that hook solved my problem plus a few revisions. On my main plugin file, however, I noticed I forgot to use require_once for the submission code since it is separate. Thanks! Commented Nov 17, 2015 at 20:11

It is not being TRUE because the action url of the comment form points to the wp-comments-post.php file at the root of the site which handle the submission. Once the submitting was successfully processed at that file the user is redirected back to the post in which the submit was done.

As the other answers had said, you are doing it wrong and you should use the available comment handling hooks to trigger the execution of your code (if at all it is needed)


I don´t want to stop you, but what you are doing here is WordPress standard functionality. Have a look at Settings -> Discussion where you will find a textarea where you can enter words which trigger a comment to need moderation.

Besides that you are probably going the wrong way. You should not just "wait" for a submit, but create a hook at the relevant places. You could use comment_post to trigger a function when a comment is posted.

  • What I am trying to do is prevent a foul comment from being submitted at all (meaning that it does not appear in any queue) and therefore display the message “You have entered forbidden text. Please consider revising.” Commented Nov 17, 2015 at 17:54
  • From a psychological point of view I wouldn´t do it that way. It just motivates people to try to get around your filter functions. Just let the comment stand in the moderation queue and delete it by time. Denying the feeling of success like in "I got around it" is helpful to get rid of trolls and foul language commenters.
    – flomei
    Commented Nov 17, 2015 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.