• I have a custom form in my WordPress web using Contact Form 7 like this:

    <label> Tu correo electrónico (requerido)
        [email* your-email default:user_email] </label>
    
    [file* archivo limit:5mb filetypes:png|jpg|jpeg]
    
    [submit "Enviar"]

    Now I want to validate the file* tag to be sure image width and height has some specific pixels.

    A have code like this in my function.php to hook a validation:

    add_filter( 'wpcf7_validate_file*', 'custom_file_validation_filter', 20, 2 );
    
    function custom_file_validation_filter( $result, $tag ) {
        $tag = new WPCF7_FormTag( $tag );
    
        if ( 'archivo' == $tag->name ) {
    
                    ====>  $img=getimagesize($file['tmp_name']); <=====
    
                    $minimum = array('width' => '640', 'height' => '480');
                    $width= $img[0];
                    $height =$img[1];
    
            if ($width < $minimum['width']) {
                $result->invalidate( $tag, "Image dimension are to small. Minimum width is {$minimum['width']}px. Uploaded image width is $width px");
            } elseif ($height <  $minimum['height']) {
                $result->invalidate( $tag, "Image dimension are to small. Minimum height is {$minimum['height']}px. Uploaded image height is $height px");
            }        
        }
    
        return $result;
    }

    My problem is the line to get the current imagesize from the WPCF7 file tag. Anybody knows how can I get it?

The topic ‘WordPress Contact Form 7 Image Size Validation’ is closed to new replies.