WebP images upload blocked

Home Forums BulletProof Security Pro WebP images upload blocked

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #39416
    Krzysztof
    Participant

    Hi!

    I have tried to upload a WebP image to make Google more happy about site optimizations, but it was blocked during the upload process. I’m wondering if BPS Pro is blocking it? If yes, than are such images dangerous? If they are not dangerous, than how can I allow such uploads? A htaccess edit?

    All the best,

    Krzysztof

    #39417
    AITpro Admin
    Keymaster

    I don’t think WebP image file types are allowed by default to be uploaded in WordPress > https://codex.wordpress.org/Uploading_Files  You would need to add that file type in the WordPress allowed file types.  Also read this article before adding WebP files to your website > https://kinsta.com/blog/webp/#how-to-use-webp-images-on-wordpress

    #39418
    Krzysztof
    Participant

    Thank you for clarification. Solved!

    It can be solved with this in functions.php

    /**
     * Sets the extension and mime type for .webp files.
     *
     * @param array  $wp_check_filetype_and_ext File data array containing 'ext', 'type', and
     *                                          'proper_filename' keys.
     * @param string $file                      Full path to the file.
     * @param string $filename                  The name of the file (may differ from $file due to
     *                                          $file being in a tmp directory).
     * @param array  $mimes                     Key is the file extension with value as the mime type.
     */
    add_filter( 'wp_check_filetype_and_ext', 'wpse_file_and_ext_webp', 10, 4 );
    function wpse_file_and_ext_webp( $types, $file, $filename, $mimes ) {
        if ( false !== strpos( $filename, '.webp' ) ) {
            $types['ext'] = 'webp';
            $types['type'] = 'image/webp';
        }
    
        return $types;
    }
    
    /**
     * Adds webp filetype to allowed mimes
     * 
     * @see https://codex.wordpress.org/Plugin_API/Filter_Reference/upload_mimes
     * 
     * @param array $mimes Mime types keyed by the file extension regex corresponding to
     *                     those types. 'swf' and 'exe' removed from full list. 'htm|html' also
     *                     removed depending on '$user' capabilities.
     *
     * @return array
     */
    add_filter( 'upload_mimes', 'wpse_mime_types_webp' );
    function wpse_mime_types_webp( $mimes ) {
        $mimes['webp'] = 'image/webp';
    
      return $mimes;
    }
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.