Wordpress Contact Form 7 custom validation rule not executed - php

Can't seem to get add_filter() to get working to add a custom validation rule to contact form 7 (v5.2.1). Tried many examples that claim to work from this website and other webs. But non seem to work so far. Can anyone shed some light on this please.
Tried below code.
function custom_text_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'your-subject') {
$value = $_POST[$name];
if (preg_match('/[\'^£$%&*()}{#~><>|=_+¬]/', $value)){
// $result->invalidate( $tag, "Invalid characters." ); // this did not work
$result['valid'] = false;
$result['reason'][$name] = 'Invalid characters';
}
}
return $result;
}
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 999, 2);
Since above code does not work, tried following.
function custom_text_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'your-subject') {
$value = $_POST[$name];
if (preg_match('/[\'^£$%&*()}{#~><>|=_+¬]/', $value)){
// $result->invalidate( $tag, "Invalid characters." ); // this did not work
$result['valid'] = false;
$result['reason'][$name] = 'Invalid characters';
}
}
//Added below lines to fire validation error no matter what. But still contact form submits successfully.
$result['valid'] = false;
$result['reason'][$name] = 'Invalid characters';
return $result;
}
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 999, 2);
Further tried different priorities to add_filter too.
add_filter('wpcf7_validate_text','custom_text_validation_filter', 1, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 2, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 10, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 20, 2);
add_filter('wpcf7_validate_text','custom_text_validation_filter', 999, 2);
Non worked

Are you doing well in your function?
The parameter $tag is an object, it seems to me.
Other than that, I don't see what is blocking :/
https://contactform7.com/2015/03/28/custom-validation/

Related

can't target gravityform input or field ID in filter

I have a filter/validation script that only accepts emails from a certain domain. This is working pretty good but I need to have it on a specific from input ID:
As per https://docs.gravityforms.com/gform_field_validation/#11-email-validation-by-third-party-api#usage :
You can also target a specific field by adding the form id and field
id after the hook name.
//The following declaration targets field 1 in form 6 add_filter(
'gform_field_validation_6_1', 'your_function_name', 10, 4 );
Currently my code looks like this:
add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
if ( ! $result['is_valid'] && $field->get_input_type() === 'email' && strpos($field->get_field_input(), 'company.com') === true) {
$result['is_valid'] = true;
$result['message'] = '';
} else {
$result['is_valid'] = false;
$result['message'] = 'E-Mail must be a company.com address';
}
return $result;
}, 10, 4 );
My form and input IDs are confirmed as 1 and 13, respectively:
When I change the code to the following, the script stops working:
add_filter( 'gform_field_validation_13_1', function ( $result, $value, $form, $field ) {
if ( ! $result['is_valid'] && $field->get_input_type() === 'email' && strpos($field->get_field_input(), 'company.com') === true) {
$result['is_valid'] = true;
$result['message'] = '';
} else {
$result['is_valid'] = false;
$result['message'] = 'E-Mail must be a company.com address';
}
return $result;
}, 10, 4 );
Is there another way I need to check my form ID? What is missing here?
From the documentation gform_field_validation_6_1 means that it targets field 1 in form 6.
You form id is 1 and input field id is 13 so the filter should be gform_field_validation_1_13.

Contact Form 7 Validate Minlength

first of all I'm using latest WordPress and CF7 version. I want to include the minlength validation for the tel field before. I know the syntax minlength=""
can be used inside the CF7, but for unknown reason, it won't work. Only maxlength="" is ok.
I already contacted the plugin support, but seems like no further response. So, I search here and found some code and I edit it so that the field will return an error if user put less than 10 characters. I put the codes inside functions.php
function custom_phone_validation($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if($name == 'Subject'){
$phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
if($phoneNumber < "9"){
$result->invalidate( $tag, "phone number is less" );
}
}
return $result;
}
add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);
The result now is, it always display the "phone number is less" even though I insert more than 9 characters. May I know what wrong and how to solve it?
as I've tested you must have your tel field [tel* phonenumber tel-503] where phonenumber is name of postfield you are posting, second issue in your code is $name=='Subject' as you are validating tel so $name will be phonenumber. So it will be like this:
function custom_phone_validation($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if($name == 'phonenumber'){
$phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
if(strlen($phoneNumber) < 9){
$result->invalidate( $tag, "phone number is less" );
}
}
return $result;
}
add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);
Your $phoneNumber is a string. You will need to get the length of the string to compare with 9.
Your code will become:
function custom_phone_validation($result,$tag){
$type = $tag['type'];
$name = $tag['name'];
if($name == 'Subject'){
$phoneNumber = isset( $_POST['phonenumber'] ) ? trim( $_POST['phonenumber'] ) : '';
if(strlen($phoneNumber) < 9){//<=====check here
$result->invalidate( $tag, "phone number is less" );
}
}
return $result;
}
add_filter('wpcf7_validate_tel','custom_phone_validation', 10, 2);
add_filter('wpcf7_validate_tel*', 'custom_phone_validation', 10, 2);

Validate lowercase input by user

Using this code which works fine if the user enters all capital letters. However, I need to automatically accept lowercase and sentence case as valid inputs by the user. Currently, this doesnt work, any ideas?
add_filter( 'gform_field_validation_17_4', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {
$arrWhitelist = array('KNITS10', 'KATNA20');
if ( $result['is_valid'] && !in_array( $value, $arrWhitelist )) {
$result['is_valid'] = false;
$result['message'] = 'Incorrect code';
}
return $result;
}
use strtoupper(), to make the users input match yours:
add_filter( 'gform_field_validation_17_4', 'custom_validation', 10, 4 );
function custom_validation( $result, $value, $form, $field ) {
$arrWhitelist = array('KNITS10', 'KATNA20');
if ( $result['is_valid'] && !in_array( strtoupper($value), $arrWhitelist )) {
$result['is_valid'] = false;
$result['message'] = 'Incorrect code';
}
return $result;
}
You could also use strcasecmp(), but the cleaner option in this scenario would be strtoupper.
The resulting code using strcasecmp would be:
function custom_validation($result, $value, $form, $field)
{
$arrWhitelist = array('KNITS10', 'KATNA20');
array_walk(
$arrWhitelist, function ($item) use ($value, $result) {
if (strcasecmp($item, $value) === 0) {
$result['is_valid'] = false;
$result['message'] = 'Incorrect code';
}
}
);
return $result;
}

Modifying plural form of views_edit in wordpress

I want to change default Draft text for my custom post type views, and I found a function that could do this:
add_filter( 'views_edit-custom_post_type_name', 'custom_draft_name', 10, 1);
if (!function_exists('custom_draft_name')) {
function custom_draft_name( $views ) {
if (isset($views['draft']) && $views['draft'] != '') {
$views['draft'] = str_replace(esc_html__('Draft', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
print_r($draft_array);
}
return $views;
}
}
The problem is that I get only singular change for this. So if I had one Draft I'll have one Unapproved. But when I have more than one I'll have two Drafts and automatically I'll have two Unapproveds. Which is terrible. It should be two unapproved. So I tried to use _n() for the plurals, by creating an array to check against with my str_replace:
add_filter( 'views_edit-custom_post_type_name', 'custom_draft_name', 10, 1);
if (!function_exists('custom_draft_name')) {
function custom_draft_name( $views ) {
if (isset($views['draft']) && $views['draft'] != '') {
$draft_array = array(esc_html__('Draft', 'custom_post_type_name'), _n('Draft', 'Drafts' , 10, 'custom_post_type_name'));
$views['draft'] = str_replace($draft_array, esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
print_r($draft_array);
}
return $views;
}
}
But I still get Unapproveds instead Unapproved.
Any help how to solve this?
** ANSWER **
And once again I found the solution immediately after I post on SO xD
Just do a wp_count_posts('custom_post_type') like
$custom_posts = wp_count_posts('custom_post_type_name');
if($custom_posts->draft == 1){
$views['draft'] = str_replace(esc_html__('Draft', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
} else{
$views['draft'] = str_replace(esc_html__('Drafts', 'custom_post_type_name'), esc_html__('Unapproved', 'custom_post_type_name'), $views['draft']);
}

How to validate email address in Wordpress with Ninja Form plugin

I'm trying to find a solution to only validate emails with .edu extension in Wordpress using Ninja Forms plugin.
With Contact Form 7 I can something like this:
function is_edu($email) {
if(substr($email, -4) == '.edu') {
return true;
} else {
return false;
};
};
function custom_email_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'your-email') { // Only apply to fields with the form field name of "your-email"
$the_value = $_POST[$name];
if(!is_edu($the_value)){
$result['valid'] = false;
$result['reason'][$name] = 'This is not a .edu address!'; // Error message
};
};
return $result;
};
add_filter('wpcf7_validate_email','custom_email_validation_filter', 10, 2); // Email field
add_filter('wpcf7_validate_email*', 'custom_email_validation_filter', 10, 2); // Required Email field
Here is a regex you can use :
if(preg_match('/^[a-zA-z0-9]+#[a-zA-z0-9]+\.edu$/', $emailaddress)) {
//valid email address, start script
}
I imagien you know how to implement this into the plugin ?

Categories