Media upload – choose only one file

It is possible to restrict the upload process to choose only one file. Now a person can select various files from pc, Im trying to find a way to restrict this feature just to one file.

Thanks in advance.

Related posts

Leave a Reply

3 comments

  1. WordPress contains 2 media up-loaders. The Flash uploader allows the selection of multiple files while the browser uploader only allows 1 file at a time.

    To disable the Flash uploader add the following filter to functions.php

    add_filter('flash_uploader', create_function('$flash', 'return false;'));
    

    EDIT

    After further investigation it’s probably not a great idea to use create_function. A beter way to remove the filter would be:

    function disable_flash_uplaoder() {
            return $flash = false;
    }
    add_filter( 'flash_uploader', 'disable_flash_uploader', 7 ); 
    
  2. The answer Chris gave is nice but doesnt really limit the upload to one file only, its just one file at a time, so the user can upload as many as he wants, but you can also limit the upload on the flash uploader to only one file using wp_handle_upload_prefilter hook, take a look at Mikes answer to a similar question.

  3. You can modify the Flash uploader to accept only one file via the BUTTON_ACTION setting. You can change this via the SWFUpload configuration (which is hardcoded in WordPress), or via the setButtonAction() method of the generated SWFUpload object – but this only works after the Flash file has loaded. I managed to do this by hooking into the swfupload_loaded_handler hook:

    var wpse15264_original_onload = SWFUpload.onload;
    SWFUpload.onload = function() {
        wpse15264_original_onload();
        swfu.addSetting( 'swfupload_loaded_handler', function() {
            this.setButtonAction( SWFUpload.BUTTON_ACTION.SELECT_FILE );
        } );
    }
    

    You can output this after the SWFUpload code via the post-html-upload-ui hook.

    This code does not add a handler to the swfupload_loaded_handler event, it replaces the current handler. This should be no problem because WordPress does not use this handler, but if it ever does in the future you should make sure to save the original handler and execute it too (like I did with the onload handler).

    Of course, this does not prevent people to use the uploader multiple times. See Bainternet’s reference for a solution to that.