0

Need help with a function that will limit upload for non admins:

  1. Check before upload if file is jpg, jpeg.
  2. Check before upload if picture is not bigger than 700kb.
  3. Limit uploaded files to 7 per normal user WordPress account.

I was searching for plugin or function that will be able to do this but no luck :(

Is it even possible to check the file before upload in wordpress?

1 Answer 1

5

You cannot check the image before uploading, as WordPress is a serverside script.

However, before inserting the image in the Media library, you have different options, as I explained in this answer, with filtering wp_handle_upload_prefilter.

In your case, the function filtering the wp_handle_upload_prefilter would be something like this:

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

    // get filesize of upload
    $size = $file['size'];
    $size = $size / 1024; // Calculate down to KB

    // get imagetype of upload
    $type = $file['type'];
    $is_image = strpos($type, 'image');

    // set sizelimit
    $limit = 700; // Your Filesize in KB

    // set imagelimit
    $imagelimit = 7;

    // set allowed imagetype
    $imagetype = 'image/jpeg';

    // query how many images the current user already uploaded
    global $current_user;
    $args = array(
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'numberposts'     => -1,
        'post_type'       => 'attachment',
        'author'          => $current_user->ID,
    );
    $attachmentsbyuser = get_posts( $args );

    if ( ( $size > $limit ) && ($is_image !== false) ) { // check if the image is small enough
        $file['error'] = 'Image files must be smaller than '.$limit.'KB';
    } elseif ( $type != $imagetype ) { // check if image type is allowed
        $file['error'] = 'Image must be ' . $imagetype . '.';
    } elseif ( count( $attachmentsbyuser ) >= $imagelimit ) { // check if the user has exceeded the image limit
        $file['error'] = 'Image limit of ' . $imagelimit . ' is exceeded for this user.';
    }
    return $file;

}
14
  • Hello thank You very much You are the best ! and it's working one more question what is: $size = $size / 1024; // Calculate down to KB
    – c64girl
    Feb 5, 2014 at 16:29
  • This is just to compare KB to KB. You could also skip this line and enter your filsizelimit in Bytes. I prefer KB because the numbers do not get that big.
    – fischi
    Feb 5, 2014 at 16:48
  • Ok thank You for fast answer and the code is working !!! Again thank You. One more question do You know how hide this option "Regenerate thumbnails" in wp-admin/upload.php from non admin users ?
    – c64girl
    Feb 5, 2014 at 18:24
  • Hm, this seems to be added by a Plugin. This Plugin does not need to be activated all the time, does it? Try deactivating it after regenerating your thumbnails.
    – fischi
    Feb 6, 2014 at 7:28
  • 1
    Hmm changed it like You posted and it still show me i have used limit 5 images as admin logged.
    – c64girl
    Feb 19, 2014 at 0:10

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.