AnswerBun.com

How can I restrict image upload dimensions for non-admin WordPress users?

WordPress Development Asked by Patrick Aziken on January 6, 2022

I want to restrict the dimensions of images uploaded by anyone who is not an administrator in WordPress to a specific size. I also want to return a specific error feedback when the image uploaded doesn’t meet the requirements.

For example: If I set the restriction to width of 500px and height of 700px, if a user tries to upload anything different from my specified width and height, it will be rejected and return a message that says, image size should be of size 500px by 700px in jpg format only.

I found a solution that only requires minimum dimensions but it’s a little bit away from what I want (How to Require a Minimum Image Dimension for Uploading?).

I have added @clayRay‘s suggestion, help check this:

add_action( 'admin_init', 'wpse_28359_block_authors_from_uploading_small_images' );

function wpse_28359_block_authors_from_uploading_small_images()
{
    if( !current_user_can( 'administrator') )
        add_filter( 'wp_handle_upload_prefilter', 'wpse_28359_block_small_images_upload' ); 
}

function wpse_28359_block_small_images_upload( $file )
{
    // Mime type with dimensions, check to exit earlier
    $mimes = array( 'image/jpeg', 'image/png', 'image/gif' );

    if( !in_array( $file['type'], $mimes ) )
        return $file;

    $img = getimagesize( $file['tmp_name'] );
    $minimum = array( 'width' => 500, 'height' => 500 );
    $maximum = array( 'width'  $maximum['width'] )
        $file['error'] = 
            'Image too large. Maximum width is ' 
            . $maximum['width'] 
            . 'px. Uploaded image width is ' 
            . $img[0] . 'px';

    elseif ( $img[1] > $maximum['height'] )
        $file['error'] = 
            'Image too large. Maximum height is ' 
            . $maximum['height'] 
            . 'px. Uploaded image height is ' 
            . $img[1] . 'px';

    return $file;
}

One Answer

I've modified this answer from brasofilo. Modify the width and height values in the following line in the below code to your needs...

    $maximum = array( 'width' => 500, 'height' => 700 );

... and then add the code to your theme's functions.php file.

add_action( 'admin_init', 'wpse_371740_block_authors_from_uploading_large_images' );

function wpse_371740_block_authors_from_uploading_large_images()
{
    if( !current_user_can( 'administrator') )
        add_filter( 'wp_handle_upload_prefilter', 'wpse_371740_block_large_images_upload' ); 
}

function wpse_371740_block_large_images_upload( $file )
{
    // Mime type with dimensions, check to exit earlier
    $mimes = array( 'image/jpeg', 'image/png', 'image/gif' );

    if( !in_array( $file['type'], $mimes ) )
        return $file;

    $img = getimagesize( $file['tmp_name'] );
    $maximum = array( 'width' => 500, 'height' => 700 );

    if ( $img[0] > $maximum['width'] )
        $file['error'] = 
            'Image too large. Maximum width is ' 
            . $maximum['width'] 
            . 'px. Uploaded image width is ' 
            . $img[0] . 'px';

    elseif ( $img[1] > $maximum['height'] )
        $file['error'] = 
            'Image too large. Maximum height is ' 
            . $maximum['height'] 
            . 'px. Uploaded image height is ' 
            . $img[1] . 'px';

    return $file;
}

Answered by clayRay on January 6, 2022

Add your own answers!

Related Questions

How to handle image resize in media_handle_sideload?

1  Asked on January 10, 2021 by user1692333

 

apiFetch security

1  Asked on January 9, 2021

 

Use cron to create a non blocking task

2  Asked on January 7, 2021 by cyclonecode

   

Creating a slider shortcode to use with bxslider

0  Asked on January 6, 2021 by devsem

   

Redirect Admin User in Dashboard

4  Asked on January 5, 2021 by plumwd

   

Adding featured image to a new post using front-end form?

1  Asked on January 3, 2021 by davood-kazemi

   

Hook when post is set from published to draft?

1  Asked on January 2, 2021 by anton-emery

       

How to execute filter with WP Cli?

0  Asked on January 2, 2021 by efbi

   

Get HTTP response code on non-2xx apiFetch request

0  Asked on January 1, 2021 by nicholas-harris

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP