Description
The gform_moderation_score
filter allows custom checks to be performed and the score to be overridden before it is saved to the entry meta.
Usage
Applies to all forms:
add_filter( 'gform_moderation_score', 'your_function_name', 10, 4 );
Parameters
Setting | Type | Details |
---|---|---|
$score | Array | An array containing the field scores and the total score. See Score Parameters. |
$entry | Entry Object | The entry currently being processed. |
$form | Form Object | The form currently being processed. |
$fields_to_analyze | Field Object[] | The fields currently being moderated. |
Score Parameters
Setting | Type | Details |
---|---|---|
$fields | Array | An array using the field ID (string) as the key to the field score (integer|float). |
$total | Integer|Float | The maximum score that was assigned to a field. |
Note: Scores are integers or floats that range from 0 (indicating non-toxic) to 1 (indicating toxic).
Examples
Check fields for URLs
add_filter( 'gform_moderation_score', function ( $score, $entry, $form, $fields_to_analyze ) {
if ( empty( $fields_to_analyze ) ) {
return $score;
}
$has_url = false;
foreach ( $fields_to_analyze as $field ) {
if ( ! in_array( $field->get_input_type(), array( 'text', 'textarea' ) ) ) {
continue;
}
$value = $field->get_value_export( $entry );
if ( empty( $value ) ) {
continue;
}
if ( preg_match( "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $value ) ) {
$has_url = true;
$score['fields'][ (string) $field->id ] = 1;
}
}
if ( $has_url ) {
$score['total'] = 1;
}
gf_moderation()->log_debug( 'gform_moderation_score: ' . print_r( $score, true ) );
return $score;
}, 10, 4 );
Placement
This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.
See also the PHP section in this article: Where Do I Put This Code?
Source Code
This filter is located in \Gravity_Forms\Gravity_Forms_Moderation\GF_Moderation::get_entry_toxicity_score()
in class-gf-moderation.php
Since
The filter was added in Moderation 1.2.1