Atenção!

Este artigo contém código PHP e destina-se a desenvolvedores. Oferecemos este código como uma cortesia, mas não fornecemos suporte para personalizações de código ou desenvolvimento de terceiros.

Para orientação extra, consulte o tutorial do WPBeginner sobre como adicionar código personalizado.

Dispensar

Descrição

The wpforms_process_filter fires after all field validation and formatting data.

Parâmetros

$fields
(array) Valores/propriedades dos campos de envio sanitizados.
$entry
(array) Variável global $_POST original.
$form_data
(array) Configurações/dados do formulário processados, preparados para uso posterior.

Fonte

wpforms/includes/class-process.php

Mais Informações

The filter fires at the very end once the field validation and formatting the data.

Exemplos

Remember to change your form ID from 817 to the form ID that you’re targeting.

This example will look to see if there is a Ratings field on this particular form and if the user doesn’t complete this field, it will default the rating to 0.

/**
 * Fires after all field validation and formatting data.
 *
 * @link  https://wpforms.com/developers/wpforms_process_filter/
 *
 * @param  array  $fields     Sanitized entry field values/properties.
 * @param  array  $entry      Original $_POST global.
 * @param  array  $form_data  Form data and settings.
 *
 * @return array 
 */

function wpf_dev_process_filter( $fields, $entry, $form_data ) {
	
	$form_id = 817; // Change form ID
	
	// Bail early if form ID does not match
	if ( $form_data[ 'id' ] != $form_id ) {
		return $fields;
	}
	
	foreach ( $fields as $field ) {
		
		// If field type is rating and it is empty, assign a value of 0
		if ( $field[ 'type' ] == 'rating' && empty( $field[ 'value' ] ) ) {
			
			$fields[$field[ 'id' ]][ 'value' ] = 0;
			
		}
		
	}
	
	return $fields;
     
}
add_filter( 'wpforms_process_filter', 'wpf_dev_process_filter', 10, 3 );

Another example could be when the Show Values tutorial has been implemented to save the value instead of the label.

/**
 * Save choices `values` instead of `labels` for the fields with `Show values` option enabled.
 *
 * @link   https://wpforms.com/developers/wpforms_process_filter/
 *
 * @param  array  $fields    Sanitized entry field. values/properties.
 * @param  array  $entry     Original $_POST global.
 * @param  array  $form_data Form data and settings.
 */

function wpf_dev_process_filter_choices_values( $fields, $entry, $form_data ) {

	if ( ! is_array( $fields ) ) {
		return $fields;
	}

	foreach ( $fields as $field_id => $field ) {
		if (
			isset( $field[ 'type' ] ) &&
			in_array( $field[ 'type' ], [ 'checkbox', 'radio', 'select' ], true ) &&
			! empty( $form_data[ 'fields' ][ $field_id ][ 'show_values' ] )
		) {
			$value_raw = ! empty( $field[ 'value_raw' ] ) ? $field[ 'value_raw' ] : '';
			$field[ 'value_raw' ] = $field[ 'value' ];
			$field[ 'value' ] = $value_raw;
			$fields[ $field_id ] = $field;
		}
	}

	return $fields;
};
add_filter( 'wpforms_process_filter', 'wpf_dev_process_filter_choices_values', 10, 3 );

Artigos de Referência