Skip to Content
Frontlane Studio
All Snippets
PHP WordPress November 4, 2024

Restrict Image Size Uploads

Explanation:

WordPress PHP

Explanation:

function restrict_image_sizes_upload($file) {
    // Array of allowed image sizes
    $allowed_sizes = array(
        array('width' => 800, 'height' => 600),
        array('width' => 1024, 'height' => 768),
        array('width' => 1200, 'height' => 900),
    );

    // Get the image size
    $image_info = getimagesize($file['tmp_name']);
    if ($image_info) {
        $image_width = $image_info[0];
        $image_height = $image_info[1];

        // Check if the image size matches any allowed size
        $is_allowed = false;
        foreach ($allowed_sizes as $size) {
            if ($image_width == $size['width'] && $image_height == $size['height']) {
                $is_allowed = true;
                break;
            }
        }

        // If the size is not allowed, return an error
        if (!$is_allowed) {
            $file['error'] = 'This image size is not allowed. Please upload an image with one of the following sizes: '
                             . implode(', ', array_map(function ($size) {
                                   return $size['width'] . 'x' . $size['height'];
                               }, $allowed_sizes));
        }
    }

    return $file;
}

add_filter('wp_handle_upload_prefilter', 'restrict_image_sizes_upload');
function restrict_image_sizes_upload($file) {
    // Array of allowed image sizes
    $allowed_sizes = array(
        array('width' => 800, 'height' => 600),
        array('width' => 1024, 'height' => 768),
        array('width' => 1200, 'height' => 900),
    );

    // Get the image size
    $image_info = getimagesize($file['tmp_name']);
    if ($image_info) {
        $image_width = $image_info[0];
        $image_height = $image_info[1];

        // Check if the image size matches any allowed size
        $is_allowed = false;
        foreach ($allowed_sizes as $size) {
            if ($image_width == $size['width'] && $image_height == $size['height']) {
                $is_allowed = true;
                break;
            }
        }

        // If the size is not allowed, return an error
        if (!$is_allowed) {
            $file['error'] = 'This image size is not allowed. Please upload an image with one of the following sizes: '
                             . implode(', ', array_map(function ($size) {
                                   return $size['width'] . 'x' . $size['height'];
                               }, $allowed_sizes));
        }
    }

    return $file;
}

add_filter('wp_handle_upload_prefilter', 'restrict_image_sizes_upload');



 $allowed_sizes array: Contains the approved image dimensions (width x height) for uploads.

 The wp_handle_upload_prefilter filter intercepts the file before it is uploaded.

 The code uses getimagesize() to get the dimensions of the uploaded image and checks against the allowed sizes.

 If the uploaded image does not match any size in the $allowed_sizes array, an error message is shown to the user.

Customization:

Modify the $allowed_sizes array to include any specific image dimensions you need to allow.