Enables SVG and SVGZ file uploads

Enables SVG and SVGZ file uploads for admins to ensure safer file handling.

PHP
/**
 * Snippet Name:     Enables SVG and SVGZ file uploads
 * Snippet Author:   coding-bunny.com
 * Description:      Enables SVG and SVGZ file uploads for admins to ensure safer file handling.
 */

function cb_allow_svg_uploads( $file_types ) {
    // Only allow admins to upload SVG files for security
    if ( current_user_can( 'administrator' ) ) {
        // Define allowed SVG file types
        $new_filetypes = array(
            'svg'  => 'image/svg+xml',
            'svgz' => 'image/svg+xml',
        );

        // Merge only new file types that are not already in the allowed list
        $file_types = array_merge( $file_types, array_diff_key( $new_filetypes, $file_types ) );
    }

    return $file_types;
}

// Hook into 'upload_mimes' to add new file types
add_action( 'upload_mimes', 'cb_allow_svg_uploads' );

How To Implement This Solution?

Leave a Reply

Your email address will not be published. Required fields are marked *

My Agile Privacy
This site uses technical and profiling cookies. You can accept, decline or customize cookies by pressing the desired buttons. By closing this policy you will continue without accepting.

Need help?

Choose one of the following options:

Powered by CodingBunny