Contact Form 7 text field validation filter hooks details. We can modify the validation function by wpcf7_validate_text
filter.
The wpcs_custom_validation_filter
hook will run during submission time. The $result
is WPCF7_Validation
object. It has invalidate
method. The invalidate method will store invalid fields when we call invalidate
method.
The wpcf7_validate_text Filter
The Contact Form 7 text field validation filter hook example. This example will check exists number in text fields. This code returns an error message Sorry… Number exists..
if a number exists in text fields.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
add_filter( 'wpcf7_validate_text', 'wpcs_custom_validation_filter', 10, 2 ); add_filter( 'wpcf7_validate_text*', 'wpcs_custom_validation_filter', 10, 2 ); function wpcs_custom_validation_filter( $result, $tag ) { $name = $tag->name; $value = isset( $_POST[$name] ) ? trim( wp_unslash( strtr( (string) $_POST[$name], "\n", " " ) ) ) : ''; if ( 'text' == $tag->basetype ) { if ( preg_match('/\d/', $value ) ) { $result->invalidate( $tag, 'Sorry... Number exists..' ); //$result->invalidate( $tag, wpcf7_get_message( 'invalid_wpcs_custom_error' ) ); } } return $result; } |
How to Change Custom Error Message From WP-Admin?
We need to hook wpcf7_messages
by filter hooks.
1 2 3 4 5 6 7 8 9 10 11 |
add_filter( 'wpcf7_messages', 'wpcf7_telephone_messages' ); function wpcf7_telephone_messages( $messages ) { return array_merge( $messages, array( 'invalid_wpcs_custom_error' => array( 'description' => __( "Text field contain number", 'wpcs-plugin' ), 'default' => __( "Sorry... Number exists..", 'wpcs-plugin' ) ) ) ); return $messages; } |
Add $result->invalidate( $tag, wpcf7_get_message( 'invalid_wpcs_custom_error' ) );
in wpcs_custom_validation_filter
function. Then go to wp-admin > contact form 7 > messages.
How to Save Data to MySQL Database?
We can save submission data by CFDB7 or CF7ADB plugin.
We can save the submission to a separate table and column automatically by the CF7ADB plugin.
Custom Plugin Development
Feel free to PM https://ciphercoin.com/service