Validate listing's description field for prohibited words

Yes, that was it Kseniia. But I would not be able to apply your suggestion without knowing about AI linked from the Support menu item on the HivePress.io ( HivePress AI Assistant )

My request to AI:

Please provide me with an example on how to use filter hivepress/v1/forms/{form_name}/errors to check listing_update form input against the list of hundreds of forbidden strings (these strings may contain quotation marks and special characters and ideally should be supplied in a file, one string per line.

Response (in my own words):

Use WP plugin Code Snippets to create new PHP snippet, pasting:

add_filter(
    'hivepress/v1/forms/listing_update/errors',
    function( $errors, $form ) {
        // Load forbidden strings from a file
        $forbidden_file = get_stylesheet_directory() . '/forbidden-strings.txt';
        
        if ( ! file_exists( $forbidden_file ) ) {
            return $errors;
        }
        
        // Read the file and get forbidden strings
        $forbidden_strings = file( $forbidden_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
        
        if ( ! $forbidden_strings ) {
            return $errors;
        }
        
        // Get form values to check
        $values = $form->get_values();
        $title = isset( $values['title'] ) ? $values['title'] : '';
        $description = isset( $values['description'] ) ? $values['description'] : '';
        
        // Combine fields to check
        $content_to_check = $title . ' ' . $description;
        
        // Convert to lowercase for case-insensitive matching
        $content_lower = strtolower( $content_to_check );
        
        // Check against forbidden strings
        foreach ( $forbidden_strings as $forbidden ) {
            $forbidden_lower = strtolower( trim( $forbidden ) );
            
            if ( ! empty( $forbidden_lower ) && strpos( $content_lower, $forbidden_lower ) !== false ) {
                $errors[] = 'Your listing contains forbidden content: "' . esc_html( $forbidden ) . '"';
                break; // Stop after first match
            }
        }
        
        return $errors;
    },
    1000,
    2
);

then upload your blocklist (one blocked string per line) to /wp-content/themes/listinghive/forbidden-strings.txt (change “listinghive” to your theme name).

It started working on the listing addition form.

Note that

  1. it may not be optimal to disclose the blocked phrase : "' . esc_html( $forbidden ) . '" to the user (especially in case listings does not require admin approval), because users can then workaround blocked string easily.

  2. case sensitivity : The example uses case-insensitive matching. Remove strtolower() if you need exact case matching

  3. partial matching : The code uses strpos() which finds partial matches. If you need exact word matching, use preg_match() instead.

Later I have enhanced the script using your AI to have more features like also exact match and regular expression blocklist and permanent + temporary banning of a user. Code of a new script: Blocklist for moderating listings - #10 by obtrusive170