We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 53127
    • 15 Posts
    Sorry, that title feels spammy in itself lol but I couldnt find a solution to this anywhere for formit specifically and, to my amazement, I was able to create one!!! Yay me! Lol although I am sure someone can refine my php even simpler but anyways... Hopefully that title is very search friendly on here.

    I am a designer/developer and my customers get legitimate companies who try and sell them their services though their website's contact forms especially for SEO, website traffic blah blah blah. Well, I figured there had to be a simple solution to check for words in the message field and either give a false confirmation and not submit the form or, as this solution does, simply do nothing which in most cases will meet my need since they'll probably just move on robot or person alike... (again I am sure someone can improve upon this)

    So basically I added a few things together and came up with this custom hook which I hope is helpful to someone else as well.

    1. I created a snippet called scumwords (so as to not be confused with badwords for eforms I believe it is)
    with the php as seen here:
    $message = $hook->getValue('message');
     
    if (strpos($message, 'website') !== false) {
        return false;
    }elseif (strpos($message, 'traffic') !== false) {
        return false;
    }else{
        return true;
    }
    

    2. Update the words "traffic" and "website" with your words.
    3. Added custom hook name "scumwords" into the hooks field, which I put it after spam, like this:
    &hooks=`spam,scumwords,email,FormItSaveForm`
    


    And viola! Those emails get ignored with spammers not really sure what is going on and all other emails go through like normal.

    What do yall think?! Again, I hope it helps someone else!
      • 3749
      • 24,544 Posts
      Great tip. smiley

      Your subject line is a little misleading, since you're not really filtering out the bad words, you're preventing posts containing them.

      Here's a slightly faster and more generic version that makes it easier to add new words:

      $scumwords = array(
          'website' => '',
          'traffic' => '',
      );
      
      $message = $hook->getValue('message');
      
      $ok = true;
        
      foreach ($scumwords as $key => $value) {
      
      if (strpos($message, $value) !== false) {
          $ok = false;
          break;
      }
      
      return $ok;


      If you wanted to actually filter out (remove) the words, the code would look like this:

      $scumwords = array(
          'website' => '',
          'traffic' => '',
      );
      
      $message = $hook->getValue('message');
      
      $message =  str_replace(array_keys($scumwords), array_values($scumwords), $message);
      /* Save message to DB here */
      return true;
      




        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • The filter itself is a nice idea, but it should not fail silently. At least an error message mentioning 'bad words' should be shown.

        $hook->addError('fieldname', 'message');
        


        leads to an output in [[!+fi.error.fieldname]]
          • 53127
          • 15 Posts
          Thank you both. The reason I don't want to show them (the potential spammer) an error is because then they either contact my customer with different words, if they're that smart or they pick up the phone and call instead... which is even worse. XD

          But to also respond to you BobRay, I did consider replacing not just the words but the entire message if those keywords were present, instead of blocking (so they would get a confirmation message and think everything was good) but did not know how. I assume to do that I would need to have a snippet/output modifier for just the message field vs a custom hook? I tried that but got nowhere...

          Ideally I would just replace the message field with "I am a spammer." which would be satisfying for me personally and then my customer would just disregard the email. wink

          Thanks for the help, I will likely use your snippet vs mine.
            • 3749
            • 24,544 Posts
            I thought about replacing the words too, but I don't think editing the $message variable would edit the "real" message. I could be wrong.

            I'm not sure how you could modify the message and have your changes make it to the DB. I guess you could save the altered message to a $_SESSION variable and alter FormIt2DB to get it from there.

              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting