Skip to content

Instantly share code, notes, and snippets.

@cfxd
Created December 25, 2014 19:45
Show Gist options
  • Save cfxd/690016c7d59317198fb6 to your computer and use it in GitHub Desktop.
Save cfxd/690016c7d59317198fb6 to your computer and use it in GitHub Desktop.
Restrict WordPress Media image upload dimensions
<?php
function restrict_image_upload_size($file) {
if(!stristr($file['type'], 'image')) { // only run on images
return $file;
}
$img = getimagesize($file['tmp_name']);
$minimum = array('width' => '1024', 'height' => '768');
$width = $img[0];
$height = $img[1];
if($file['error'] != '0') { // there's already an error
return $file;
}
if(defined('WP_IMPORTING')) {
return $file;
}
if($width < $minimum['width'] || $height < $minimum['height']) {
$file['error'] = "This image must be a minimum of {$minimum['width']}px x {$minimum['height']}px; the uploaded image is only {$width}px x {$height}px";
}
return $file;
}
add_filter('wp_handle_upload_prefilter', 'restrict_image_upload_size');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment