In Gravity Forms I’d like to include a small keyword checker that is checking the user’s message for “spammy” words and stops sending the entry to the admin. For this a hidden field changes it’s value. I have this code included in my functions.php so far

function strpos_arr($haystack, $needle) {
if(!is_array($needle)) $needle = array($needle);
foreach($needle as $what) {
    if(($pos = stripos($haystack, $what))!==false) return $pos;
}
return false;
}

/*
* Our bad words validation function
*/


add_action('gform_pre_submission_1', 'keywords_check'); 

function keywords_check($validation_result){
$form = $validation_result["form"];


$stop_words = array(
    'outsource', 
    'Madam', // this covers all variations of 'Sir/Madam' 'Sir /Madam' 'Sir/ Madam' 'Sir / Madam' etc
    'SEO',
    'long term relationship',
);

$stop_id = array();

foreach($_POST as $id => $post)
{
    if(strpos_arr($post, $stop_words))
    {
        /*
         * We have a match so store the post ID so we can count it
         */ 
         $stop_id[] = $id;
    }
}

if(sizeof($stop_id) > 0)
{
    $validation_result['is_valid'] = false;


    $_POST['input_55'] = "No";

}

}

There seems to be a problem with the part in the very beginning of this code, especially with

foreach($needle as $what) {
    if(($pos = stripos($haystack, $what))!==false) return $pos;
}

After sending the form I get the following warning

Warning: stripos() expects parameter 1 to be string, array given on line 109

I have several php noob trial and error hours behind me before asking this question. Can anyone help me out with this?

Read more here: Keyword checking in Gravity Forms


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: