Snippets: Allow SVG Uploads in WordPress

By default SVG files are not allowed in WordPress for security reasons so they can't be uploaded, however you can hook into the "upload_mimes" filter to add support for svg file types. This snippet has an extra security check in place so that only site admins can upload SVG's but as always if you are going to upload SVG files to your site make sure to review the file code to ensure there isn't anything malicious before uploading it.

But before you go adding any code to your site we would highly recommend you instead use the Safe SVG plugin, especially if you are working with a client site.

/**
 * Allow SVG uploads.
 *
 * @link https://wpexplorer-themes.com/total/snippets/allow-svg-uploads-wordpress/
 */
add_filter( 'upload_mimes', function( $mimes = array() ) {
	if ( current_user_can( 'administrator' ) ) {
		$mimes['svg'] = 'image/svg+xml';
		$mimes['svgz'] = 'image/svg+xml';
	}
	return $mimes;
}, 99 );

Snippet Type: PHP

Posted Under: Other

All PHP snippets should be added via a child theme's functions.php file or via the Code Snippets Plugin (or alternative plugin)