Home / Most Popular / Allow SVG Files Upload
Duplicate Snippet

Embed Snippet on Your Site

Allow SVG Files Upload

Add support for SVG files to be uploaded in WordPress media.

46k
Code Preview
php
<?php
/**
 * Allow SVG uploads for administrator users.
 *
 * @param array $upload_mimes Allowed mime types.
 *
 * @return mixed
 */
add_filter(
	'upload_mimes',
	function ( $upload_mimes ) {
		// By default, only administrator users are allowed to add SVGs.
		// To enable more user types edit or comment the lines below but beware of
		// the security risks if you allow any user to upload SVG files.
		if ( ! current_user_can( 'administrator' ) ) {
			return $upload_mimes;
		}
		$upload_mimes['svg']  = 'image/svg+xml';
		$upload_mimes['svgz'] = 'image/svg+xml';
		return $upload_mimes;
	}
);
/**
 * Add SVG files mime check.
 *
 * @param array        $wp_check_filetype_and_ext Values for the extension, mime type, and corrected filename.
 * @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 string[]     $mimes Array of mime types keyed by their file extension regex.
 * @param string|false $real_mime The actual mime type or false if the type cannot be determined.
 */
add_filter(
	'wp_check_filetype_and_ext',
	function ( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) {
		if ( ! $wp_check_filetype_and_ext['type'] ) {
			$check_filetype  = wp_check_filetype( $filename, $mimes );
			$ext             = $check_filetype['ext'];
			$type            = $check_filetype['type'];
			$proper_filename = $filename;
			if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
				$ext  = false;
				$type = false;
			}
			$wp_check_filetype_and_ext = compact( 'ext', 'type', 'proper_filename' );
		}
		return $wp_check_filetype_and_ext;
	},
	10,
	5
);

Comments

Add a Comment

  1. Could I simply add other file types to this snippet to allow other files not permitted by WordPress? For example, most of my clients need CAD/CAM file types used in engineering and manufacturing. File types such as .dxf and .dwg. Could I simply add…

    $upload_mimes[‘dxf’] = ‘image/x-dxf’;
    $upload_mimes[‘dwg’] = ‘image/x-dwg’;

      1. Hi Tim & Jacqueline,

        Yes, you can use this filter to add support for multiple files but ideally you will use similar checks to ensure the mime type of the file is met same way we do for the svg in the 2nd part of the snippet.

        1. Thanks for the info. I’d like to use this for uploading font files. Would you be able to show me how that would look? It’s a little over my head. 😉 Thank you!!

          1. Hi Jacqueline,

            You have to find the mime types for the font files and use that to extend the upload mimes the way we do it for SVG. For ttf/otf you can use something like this:

            $upload_mimes[‘ttf’] = ‘font/ttf’;
            $upload_mimes[‘otf’] = ‘font/otf’;

            Feel free to reach out using the Contact page if you need more help.