Filter
uncanny-automator
automator_cf7_submitted_field_value
Filters the submitted value of a Contact Form 7 field before it's processed by the Automator.
add_filter( 'automator_cf7_submitted_field_value', $callback, 10, 3 );
Description
This filter allows developers to modify submitted Contact Form 7 field values before they are processed by the plugin. It fires after a form has been submitted and the data is being prepared for further use. Developers can use this hook to sanitize, transform, or enrich field values based on the tag name and all posted data.
Usage
add_filter( 'automator_cf7_submitted_field_value', 'your_function_name', 10, 3 );
Parameters
-
$value(mixed) - This parameter contains the current value of a Contact Form 7 field.
-
$tag(mixed) - This parameter represents the actual value submitted for a specific Contact Form 7 field.
-
$posted_data(mixed) - This parameter contains the name of the form field whose value is being processed.
Return Value
The filtered value.
Examples
<?php
/**
* Example function to modify the submitted value of a Contact Form 7 field.
* This function will append a prefix to the submitted value of a specific field.
*
* @param mixed $value The original submitted value of the field.
* @param mixed $tag The Contact Form 7 tag object for the field.
* @param mixed $posted_data The entire array of posted data from the form.
*
* @return mixed The modified field value.
*/
function my_automator_modify_cf7_field_value( $value, $tag, $posted_data ) {
// Check if the current field's name is 'your-custom-field-name'
// Replace 'your-custom-field-name' with the actual name of your CF7 field.
if ( isset( $tag->name ) && 'your-custom-field-name' === $tag->name ) {
// Only append the prefix if the value is not empty.
if ( ! empty( $value ) ) {
$prefix = 'PROCESSED_';
$value = $prefix . $value;
}
}
// Return the (potentially modified) value.
return $value;
}
// Add the filter with the correct number of arguments (3: $value, $tag, $posted_data).
add_filter( 'automator_cf7_submitted_field_value', 'my_automator_modify_cf7_field_value', 10, 3 );
?>
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/integrations/contact-form7/tokens/cf7-tokens.php:142
public function get_data_from_contact_form( WPCF7_ContactForm $contact_form ) {
$data = array();
$posted_data = array(
'POST' => wp_unslash( $_POST ), // phpcs:ignore
'FILES' => wp_unslash( $_FILES ), // phpcs:ignore
);
if ( $contact_form instanceof WPCF7_ContactForm ) {
$tags = $contact_form->scan_form_tags();
foreach ( $tags as $tag ) {
if ( empty( $tag->name ) ) {
continue;
}
$is_multiple = in_array( 'multiple', $tag->options, true ) ? true : false;
$array_data_types = apply_filters( 'automator_cf7_data_type_of_array', array( 'checkbox' ), $tag, $contact_form );
if ( $is_multiple || in_array( $tag->type, $array_data_types, true ) ) {
$request_tag_name = automator_filter_input_array( $tag->name, INPUT_POST );
} elseif ( 'file' === $tag->type ) {
$request_tag_name = '';
$file = isset( $_FILES[ $tag->name ] ) && isset( $_FILES[ $tag->name ]['name'] ) ? filter_var( wp_unslash( $_FILES[ $tag->name ]['name'] ), FILTER_SANITIZE_FULL_SPECIAL_CHARS ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing
$request_tag_name = $file ? basename( $file ) : '';
} elseif ( 'textarea' === $tag->type ) {
// Use textarea sanitization for textarea field.
$request_tag_name = sanitize_textarea_field( filter_input( INPUT_POST, $tag->name, FILTER_DEFAULT ) );
} else {
$request_tag_name = automator_filter_input( $tag->name, INPUT_POST );
// Try and catch array data.
if ( empty( $request_tag_name ) ) {
$request_tag_name = automator_filter_input_array( $tag->name, INPUT_POST );
}
}
$pipes = $tag->pipes;
$value = ! empty( $request_tag_name ) ? $request_tag_name : '';
if ( WPCF7_USE_PIPE && $pipes instanceof WPCF7_Pipes && ! $pipes->zero() ) {
if ( is_array( $value ) ) {
$new_value = array();
foreach ( $value as $v ) {
$new_value[] = $pipes->do_pipe( wp_unslash( $v ) );
}
$value = $new_value;
} else {
$value = $pipes->do_pipe( wp_unslash( $value ) );
}
}
$data[ $tag->name ] = apply_filters( 'automator_cf7_submitted_field_value', $value, $tag, $posted_data );
}//end foreach
return $data;
}//end if
}