0

I quite new in wordpress , and currently studying it. I am trying to build a kind of blog for a project and i am stuck in some part. I've made a search on this website and it seems that nobody has ask this question or maybe i am not searching the right posts: I want images uploaded by whatever user,except admin, from media library to be displayed in the frontend to a specific size only for example 600x900 and additionally I want to restrict users that upload their photos through media library to some specific size, lets say if the files are lesser 600x900 it is rejected and whatever is above goes through.

Anyone can help please? Is there a plugin that can do this or some lines of codes which i can insert into functions.php?

Thanks

1 Answer 1

0

Add this code to your theme's functions.php file, and it will limit image dimentions

add_filter('wp_handle_upload_prefilter','tc_handle_upload_prefilter');
function tc_handle_upload_prefilter($file)
{

    $img=getimagesize($file['tmp_name']);
    $minimum = array('width' => '600', 'height' => '900');
    $width= $img[0];
    $height =$img[1];

    if ($width < $minimum['width'] )
        return array("error"=>"Image dimensions are too small. Minimum width is {$minimum['width']}px. Uploaded image width is $width px");

    elseif ($height <  $minimum['height'])
        return array("error"=>"Image dimensions are too small. Minimum height is {$minimum['height']}px. Uploaded image height is $height px");
    else
        return $file; 
}
2
  • Thx Tejas, I'll try and give you feedback
    – goochie
    Commented Jul 23, 2017 at 18:16
  • hello , Tejas works like a charm. Thx again for the help.
    – goochie
    Commented Aug 4, 2017 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.