SVG upload does not work

Question

WordPress doesn’t allow to upload SVG files by default.

Add svg file type in allowed file types list

function wp_mime_types($mimes)
{
    $mimes['svg'] = 'image/svg+xml';
    return $mimes;
}
add_filter('upload_mimes', 'wp_mime_types');

Fix for displaying svg files in media library

function wp_fix_svg() {
    echo '<style type="text/css">
        .attachment-266x266, .thumbnail img {
         width: 100% !important;
         height: auto !important;
        }
        </style>';
}
add_action( 'admin_head', 'wp_fix_svg' );

Attempt to determine the real file type of a file

function wp_check_filetype($data, $file, $filename, $mimes) 
{
    global $wp_version;
    if( $wp_version !== '4.7.1' ){
        return $data;
    }

    $filetype = wp_check_filetype( $filename, $mimes );

    return [
        'ext'             => $filetype['ext'],
        'type'            => $filetype['type'],
        'proper_filename' => $data['proper_filename']
    ];
}
add_filter( 'wp_check_filetype_and_ext', 'wp_check_filetype', 10, 4 );

0
Ulrich Sommer 4 weeks 2021-05-17T01:30:43-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse