How to Block Spam Words in WPForms Without Premium Version

How to Block Spam Words in WPForms Without Premium Version

Spam submissions cluttering your WPForms? Fret not! With a simple tweak, you can effectively block spam words without the need for the premium version.Are you tired of sifting through form submissions cluttered with spammy offers and promotions? In today’s digital landscape, every cent counts, and every interaction matters.

But fear not! There’s a cost-effective solution to combat spam and maintain the integrity of your form submissions without breaking the bank Follow these steps to reclaim control over your form submissions:

Important: Before proceeding with any code modifications, make sure to back up your website to prevent data loss in case of any unexpected issues.

Step 1: Access Your WordPress Dashboard

  • Log in to your WordPress admin dashboard using your credentials.

Step 2: Navigate to the WPForms Plugin

  • From the WordPress dashboard, navigate to the “WPForms” menu item on the left sidebar.
  • Click on “All Forms” to view a list of your existing forms.

Step 3: Find the Form ID

  • Identify the form you want to apply the word blocking to.
  • Wpform Dashboard and copy ids from here .

Step 4: Update the Code with the Form ID

  • Take note of the form ID for the form you want to apply the word blocking to.
  • Open the “functions.php” file of your theme in the theme editor and paste the code at the end.

Step 5: Integrate Form ID into Code

  • In the $form_ids_to_validate array, add the form ID(s) you noted down in Step 3.
  • Ensure that each form ID is separated by a comma and enclosed in single quotes
function custom_wpforms_validate( $fields, $entry, $form_data ) {
    if ( ! defined( 'WP_DEBUG' ) ) {
        define( 'WP_DEBUG', true );
    }
    if ( ! defined( 'WP_DEBUG_LOG' ) ) {
        define( 'WP_DEBUG_LOG', true );
    }

    // Log the form ID being processed
    error_log( 'Validating form ID: ' . $form_data['id'] );

    // List of form IDs to apply the validation to
    $form_ids_to_validate = array( 5846, 5526, 5447 );

    if ( in_array( $form_data['id'], $form_ids_to_validate ) ) {
        $blocked_words = array(
            'job', 'offer', 'seo', 'service', 'promotion', 'free', 'money', 'cash',
            'earn', 'income', 'investment', 'loan', 'credit', 'mortgage', 'insurance',
            'discount', 'deal', 'bargain', 'cheap', 'save', 'winner', 'winning',
            'prize', 'bonus', 'gift', 'click', 'buy', 'purchase', 'order', 'subscribe',
            'signup', 'sign up', 'sale', 'special', 'urgent', 'limited', 'hurry',
            'act now', 'exclusive', 'trial', 'risk-free', 'guarantee', 'offer expires'
        );

        foreach ( $fields as $field_id => $field ) {
            foreach ( $blocked_words as $word ) {
                if ( stripos( $field['value'], $word ) !== false ) {
                    error_log( 'Blocked word found: ' . $word );
                    wpforms()->process->errors[$form_data['id']][$field_id] = 'This field contains forbidden words.';
                    break;
                }
            }
        }
    }
}

add_action( 'wpforms_process', 'custom_wpforms_validate', 10, 3 );

 

Step 6: Test Your Forms

  • Now that the code is added, it’s time to test your forms.
  • Submit a form with values containing blocked words to ensure the validation works as expected.

Step 7: Monitor Form Submissions

  • Regularly monitor your form submissions to ensure spam words are effectively blocked.
  • Adjust the list of blocked words as needed to keep up with changing spam trends.

Conclusion

We hope this code and guide will work for you and help you keep your WPForms submissions clean from spam words. By following these steps, you can save time and maintain the quality of your data without incurring additional costs. However, if you still face any issues or need further assistance, we are here to help you. Feel free to reach out with any questions or concerns, and we’ll be happy to assist you!