Custom Fields
Re-Envisioned
Support General How to prevent uploads for plupload_image field type?

This topic contains 3 replies, has 2 voices, and was last updated by  tgarda 2 years, 9 months ago.

  • Creator
    Topic
  • #4910

    tgarda
    Participant

    Hi Anh,

    I am using Metabox 4.10.1. I defined a field like this:

        array(
            'name'             => esc_html__( 'Image Upload', 'textdomain' ),
            'id'               => 'myprefix_feature1_plupload',
            'type'             => 'plupload_image',
            'desc'  => __( 'Upload a square/circluar image image only!', 'textdomain' ),
    	'force_delete'     => true,
            'max_file_uploads' => 2,
    	'max_status'       => true,
        ),

    I would like to prevent uploading images to this field when they exceed certain sizes. Is that possible? If yes, can you give me some guidance?

    I can check the image size with $file[‘size’] in a ‘wp_handle_upload_prefilter’ hook. My problem is that stopping the upload with adding $file[‘error’] does not take effect. It actually causes the GUI to hang.

    Thank you.

    ==============================================================

    FYI, I tried using the ‘wp_handle_upload_prefilter’ hook in functions.php (see below), but that resulted in the field HTML ‘hanging’ on the GUI, while Chrome debugger showed repeated calls to /wordpres/wp-admin/admin-ajax.php

    function myprefix_limit_file_upload($file) {
    
    // Calculate the image size in KB
    $image_size = $file['size']/1024;
    
    // File size limit in KB
    $limit = 200;
    
    if ( ( $image_size > $limit ) )
            $file['error'] = 'Your picture is too large. It has to be smaller than '. $limit .'KB';
    
    return $file;
    
    }
    add_filter('wp_handle_upload_prefilter', 'myprefix_limit_file_upload');
Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #4911

    tgarda
    Participant

    Forgot to mention that setting upload_max_filesize in php.ini is not sufficient for me. I want to set different size limits based on file type (e.g. images / videos / other types should have different limitations.) I know how to check the file type. The only piece missing is stopping the file upload.

    #4914

    Anh Tran
    Keymaster

    Hi,

    We used to allow users to set parameters for plupload, but we removed that part in version 4.9.x to make all the media fields use the same codebase. There were almost no users used that.

    I will check on this to see if we can add something back.

    #4930

    tgarda
    Participant

    Hi Anh,

    Thank you for the feedback! In the meantime, I implemented a solution to automatically resize the original images after uploading if they are too large using the wp_handle_upload filter. That works and I do not need the functionality to stop uploading. It is probably a good practice not to delete images anyway, you never know what other plugins or themes expect in the Media Library. So this thread can be closed as far as I am concerned.

    However, I will open another thread for the functionality of propagating an error about exceeding the max image size limit when trying to upload a file and displaying the error on the GUI in the plupload_image field. Perhaps the way to do that is to set $file[‘error’] in the same wp_handle_upload_prefilter as in my earlier example in this thread. Perhaps there is a better way to do it. In any case, thanks again for your feedback here!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘How to prevent uploads for plupload_image field type?’ is closed to new replies.