How to Block Form Submissions Containing Profanity

Would you like to block your form submissions containing profanity? You can easily create a list of expletive words that would stop any form submission if they are found inside your Paragraph Text or in a Single Line Text field using a small code snippet. In this tutorial, we’ll walk you through the steps on how to achieve this with PHP.

Creating your form

First, you’ll need to create a new form and add either a Paragraph Text or a Single Line Text.

If you need any help in creating a form, please see this documentation.

add a single or paragraph text form field to your new form

Blocking submissions containing profanity

Next, come up with a list of words you want to add to your profanity filter. Once you’ve identified the words you want to block you’ll need to add one of these snippets to your site. If you need any help in adding snippets to your site, please see this documentation.

You’ll just add each word to your $blocked_words separated by a comma.

Paragraph Text Form Field

/**
 * Prevent profanity words from Paragraph Text form fields.
 *
 * @link https://wpforms.com/developers/how-to-block-form-submissions-containing-profanity/
 */

function wpf_dev_profanity_filter_paragraph( $field_id, $field_submit, $form_data ) {

        // Create your list of profanity words separated by commas
	$blocked_words = array( 
		'badword1', 
		'badword2'
	);

    foreach( $blocked_words as $word ) {
        if(strpos($field_submit, $word) !== FALSE ) {
            wpforms()->process->errors[ $form_data[ 'id' ] ][ $field_id ] = esc_html__( 'No profanity allowed.', 'plugin-domain' );
            return;
        }
    }

}

add_action( 'wpforms_process_validate_textarea', 'wpf_dev_profanity_filter_paragraph', 10, 3 );

Using this snippet will block submissions containing profanity inside the paragraph text form field

Single Line Text Form Field

/**
 * Prevent profanity words from Single Line Text form fields.
 *
 * @link https://wpforms.com/developers/how-to-block-form-submissions-containing-profanity/
 */

function wpf_dev_profanity_filter_single_text( $field_id, $field_submit, $form_data ) {

        // Create your list of profanity words separated by commas
	$blocked_words = array( 
		'badword1', 
		'badword2'
	);

    foreach( $blocked_words as $word ) {
        if(strpos($field_submit, $word) !== FALSE ) {
            wpforms()->process->errors[ $form_data[ 'id' ] ][ $field_id ] = esc_html__( 'No profanity allowed.', 'plugin-domain' );
            return;
        }
    }

}

add_action( 'wpforms_process_validate_text', 'wpf_dev_profanity_filter_single_text', 10, 3 );

Rich Text Form Field

/**
 * Prevent profanity words from Rich Text form fields.
 *
 * @link https://wpforms.com/developers/how-to-block-form-submissions-containing-profanity/
 */

function wpf_dev_profanity_filter_rich_text( $field_id, $field_submit, $form_data ) {

        // Create your list of profanity words separated by commas
	$blocked_words = array( 
		'badword1', 
		'badword2'
	);

    foreach( $blocked_words as $word ) {
        if(strpos($field_submit, $word) !== FALSE ) {
            wpforms()->process->errors[ $form_data[ 'id' ] ][ $field_id ] = esc_html__( 'No profanity allowed.', 'plugin-domain' );
            return;
        }
    }

}

add_action( 'wpforms_process_validate_richtext', 'wpf_dev_profanity_filter_rich_text', 10, 3 );

And that’s it! You’ve now created and implemented a profanity filter for your Single Line and Paragraph Text form fields. Would you like to block URLs from being entered into your form? Check out our tutorial on How to Block URLs Inside the Form Fields.

Reference Actions