Make WordPress Core


Ignore:
Timestamp:
03/25/2025 02:04:47 PM (3 months ago)
Author:
adamsilverstein
Message:

Media: prevent uploading image types the server doesn’t support.

Normalize behavior between uploading in the media library and uploading directly to the block editor. Now, when uploading an image with a mime type the server does not support (either in the media library or the block editor), the user will see an error message “This image cannot be processed by the web server. Convert it to JPEG or PNG before uploading”.

Alos, add a new filter wp_prevent_unsupported_mime_type_uploads which determines whether the server should prevent uploads for image types it doesn't support. The default value is true and the filter also receives the uploaded image mime type.

Props: joomskys, adamsilverstein, azaozz, swissspidy, joemcgill, flixos90, audrasjb. 

Fixes #61167

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/media.php

    r59767 r60084  
    21952195    }
    21962196
    2197     // Check if WebP images can be edited.
    2198     if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
    2199         $plupload_init['webp_upload_error'] = true;
    2200     }
    2201 
    2202     // Check if AVIF images can be edited.
    2203     if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {
    2204         $plupload_init['avif_upload_error'] = true;
     2197    /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */
     2198    $prevent_unsupported_uploads = apply_filters( 'wp_prevent_unsupported_mime_type_uploads', true, null );
     2199
     2200    if ( $prevent_unsupported_uploads ) {
     2201        // Check if WebP images can be edited.
     2202        if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) ) ) {
     2203            $plupload_init['webp_upload_error'] = true;
     2204        }
     2205
     2206        // Check if AVIF images can be edited.
     2207        if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {
     2208            $plupload_init['avif_upload_error'] = true;
     2209        }
    22052210    }
    22062211
Note: See TracChangeset for help on using the changeset viewer.