Salman Aziz
Salman Aziz

Reputation: 466

"wpcf7_validate_text" filter stopped working after Contact Form 7 plugin update to 5.6

I was using wpcf7_validate_text for all input type text as a custom validation in function.php, however after updating the form i could not see the my custom validation on input type text and textarea.

Can you please help how can i update those again. Code is given below

/* Code in Function.php */
    add_filter( 'wpcf7_validate_text', 'custom_firstname_confirmation_validation_filter', 5, 2  );
    add_filter( 'wpcf7_validate_text*', 
            'custom_firstname_confirmation_validation_filter', 5, 2  );
            
function custom_firstname_confirmation_validation_filter( $result, $tag ) {
    if ( 'firstname' == $tag->name ) { 
        $firstname = isset( $_POST['firstname'] ) ? trim( $_POST['firstname'] ) : '';

    if(empty($firstname)) {
        $msg = 'first name is Required';
        $result->invalidate( $tag, $msg);
    }
    else if (strlen((string)$firstname) < 6 || strlen((string)$firstname) > 12) {
          $msg = 'first name is not valid';
          $result->invalidate( $tag, $msg);
      }
    }

    return $result;
}
<p>*All fields are required</p>
[response]

[select* inquiry "Business Enquiry" "Media Enquiry" ]
[phonetext phone ]
[text* phonenum class:phonenum placeholder "Phone number" ]
[select* title "Mr" "Ms" "Mrs"]
[email* email placeholder "Email" ]
[text* firstname placeholder "First Name" ]
[text* lastname placeholder "Last Name" ]
[textarea textarea-854 placeholder "Enter your message here"]
[checkbox checkbox-57 checked id:chk1 "" ]
[acceptance acceptance624 id:iagree ]
[submit class:btn-theme class:btn-submit "Enquire"]

Upvotes: 2

Views: 2277

Answers (1)

Howard E
Howard E

Reputation: 5659

If your code above is exactly how you have it, then you have the return in the wrong page, and unless your tag matches, you'll get an error.

function custom_firstname_confirmation_validation_filter( $result, $tag ) {
    if ( 'firstname' === $tag->name ) {
        $first_name = isset( $_POST['firstname'] ) ? trim( $_POST['firstname'] ) : '';
        if ( empty( $first_name ) ) {
            $msg = 'First name is required';
            $result->invalidate( $tag, $msg );
        }
    }
    return $result;
}

Upvotes: 1

Related Questions