apply_filters( ‘wp_handle_upload_prefilter’, array $file )

Filter data for the current file to upload.


An array of data for a single file.

More Information

When you upload Media from your WordPress admin dashboard, wp_handle_upload is called once for each file the user specified. wp_handle_upload_prefilter is an admin filter that is called by the wp_handle_upload function. The single parameter, $file, represent a single element of the $_FILES array. The wp_handle_upload_prefilter provides you with an opportunity to examine or alter the filename before the file is moved to its final location.

Using this, in conjunction with the upload_dir, you can dynamically determine which directory to upload to, based on the files you upload.

add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );

function custom_upload_filter( $file ) {
$file['name'] = 'wordpress-is-awesome-' . $file['name'];
return $file;





User Contributed Notes

  1. Skip to note 5 content

    Example: Prevent images larger than 200kb from being uploaded.

    function plugin_prefix_max_image_size( $file ) {
      $limit    = 200;
      $size     = $file['size'] / 1024;
      $is_image = strpos( $file['type'], 'image' ) !== false;
      if ( $is_image && $size > $limit ) {
        $file['error'] = "Image files must be smaller than {$limit}kb";
      return $file;
    add_filter( 'wp_handle_upload_prefilter', 'plugin_prefix_max_image_size' );
  2. Skip to note 7 content

    Example: prepend all uploaded files with wordpress-is-awesome-

    add_filter( 'wp_handle_upload_prefilter', function( $file ) {
        $file['name'] = 'wordpress-is-awesome-' . $file['name'];
        return $file;
    } );

    (According to this code was originally in the docs. I’m adding it here again since I can’t find it anywhere else.)

You must log in before being able to contribute a note or feedback.