Advertisement
CodeDropz

Ultimate add-on for contact form 7 - conditional (custom validation)

May 18th, 2023 (edited)
1,110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.97 KB | None | 0 0
  1. if( function_exists( 'codeDropz_dnd_upload_cf7' ) ){
  2.         remove_filter('wpcf7_validate_mfile*', array( codeDropz_dnd_upload_cf7(), 'dnd_upload_cf7_validation_filter' ), 10, 2 );
  3.         add_filter('wpcf7_validate_mfile*', 'custom_cf7_validate_fields', 10, 2 );
  4.     }
  5.  
  6.     function custom_cf7_validate_fields( $result, $tag ){
  7.  
  8.         // Don't validate if license-info is "TYPE IT IN"
  9.         $condition = 'License-Info';
  10.         if( isset( $_POST[ $condition ] ) && $_POST[ $condition ] == 'Type it in' ){
  11.             return $result;
  12.         }
  13.  
  14.         // Proceed with validation
  15.         $multiple_files = ( isset( $_POST[ $tag->name ] ) ? array_map( 'sanitize_text_field', $_POST[ $tag->name ] ) : null );
  16.         if( ( is_null( $multiple_files ) || count( $multiple_files ) == 0 ) && $tag->is_required() ) {
  17.             $result->invalidate( $tag, wpcf7_get_message( 'invalid_required' ) );
  18.             return $result;
  19.         }
  20.        
  21.         return $result;
  22.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement