简体   繁体   中英

Wordpress Contact Form 7 custom validation rule not executed

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM