admin管理员组

文章数量:1122832

Could someone help me to solve the problem. I would like the system work like bellow.

  • If both fields are empty = invalid.
  • If any of the fields is completed = valid
add_filter('wpcf7_validate_text', 'or_input_validation_filter', 20, 2);

function or_input_validation_filter($result, $tag) {

    if ('your-link' == $tag->name) {
        $your_link = isset($_POST['your-link']) ? trim($_POST['your-link']) : '';
        $your_file = isset($_FILES['your-file']) ? trim($_FILES['your-file']['name']) : '';
        
        if($your_link == '' && $your_file == '') {
            $result->invalidate($tag, "One of the fields has not been completed?");
        }
    }
    return $result;
}

Could someone help me to solve the problem. I would like the system work like bellow.

  • If both fields are empty = invalid.
  • If any of the fields is completed = valid
add_filter('wpcf7_validate_text', 'or_input_validation_filter', 20, 2);

function or_input_validation_filter($result, $tag) {

    if ('your-link' == $tag->name) {
        $your_link = isset($_POST['your-link']) ? trim($_POST['your-link']) : '';
        $your_file = isset($_FILES['your-file']) ? trim($_FILES['your-file']['name']) : '';
        
        if($your_link == '' && $your_file == '') {
            $result->invalidate($tag, "One of the fields has not been completed?");
        }
    }
    return $result;
}
Share Improve this question asked May 24, 2022 at 18:25 RobertRobert 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I solved my problem

add_filter('wpcf7_validate_text', 'or_input_validation_filter', 20, 2);

function or_input_validation_filter($result, $tag) {

    if ('your-link' == $tag->name) {
        $your_link = isset($_POST['your-link']) ? trim($_POST['your-link']) : '';
        $your_file = (!empty($_FILES["file-image"]["name"])) ? trim($_FILES['file-image']['name']) : '';

        if($your_link == '' && $your_file == '') {
            $result->invalidate($tag, "One of the fields has not been completed?");
        }
           
    }
    return $result;
}

本文标签: phpContact Form 7 Wordpresschecking a few fieldsif empty then invalid