so users can upload images on my website and I want to put some restrictions on these images. The catch is that these image restriction can only apply on one page, being the edit product page. I already tried checking by template and ID of the page but this page lives below a parent page so that did not work.
I currently have the code below but now it is rejecting all images and not displaying the correct message. Anyone that can help me out?
function resize_image_resolution($file) {
if (is_user_wcmp_vendor(get_current_user_id()) && isset($_SERVER['REQUEST_URI']) && false !== strpos( $_SERVER['REQUEST_URI'], 'edit-product' ) ) {
$image = getimagesize($file['tmp_name']);
$minimum = array(
'width' => '1080', //set your minimum
'height' => '1080'
);
$maximum = array(
'width' => '12000', //set your maximum
'height' => '12000'
);
$image_width = $image[0];
$image_height = $image[1];
$too_small = "Image dimensions are too small.";
$too_large = "Image dimensions are too large.";
if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
$file['error'] = $too_small;
return $file;
} elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
$file['error'] = $too_large;
return $file;
} else {
return $file;
}
}
}
add_filter(‘wp_handle_upload_prefilter’, ‘resize_image_resolution’);
2
Answers
Thanks everyone for helping me. Eventually I have been able to get it working with following code:
See if this works for you. It would appear there are a few typos, and you should return $file at the end.