How to restrict to upload specific image size on custom upload fields on Wc vendor form

enter image description here

What i am trying to do is to upload allow only a specific sized image for these specific fields. but i tried with this code problem is it applies on every upload image field.

// check for file upload size //
{
    if( !current_user_can( 'administrator') )
        add_filter( 'wp_handle_upload_prefilter', 'mdu_validate_image_size' ); 
}
add_filter('wp_handle_upload_prefilter','mdu_validate_image_size');
function mdu_validate_image_size( $file ) {
    $image = getimagesize($file['tmp_name']);
    $minimum = array(
        'width' => '200',
        'height' => '140'
    );
    $maximum = array(
        'width' => '200',
        'height' => '140'
    );
    $image_width = $image[0];
    $image_height = $image[1];
$too_small = "Image dimensions are too small. Minimum size is {$minimum['width']} by {$minimum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";
$too_large = "Image dimensions are too large. Maximum size is {$maximum['width']} by {$maximum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";

if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
    // add in the field 'error' of the $file array the message 
    $file['error'] = $too_small; 
    return $file;
}
elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
    //add in the field 'error' of the $file array the message
    $file['error'] = $too_large; 
    return $file;
}
else
    return $file;

}

Read more here:: How to restrict to upload specific image size on custom upload fields on Wc vendor form

Leave a Reply

Your email address will not be published. Required fields are marked *