Skip to content

Instantly share code, notes, and snippets.

@nextab
Last active June 16, 2024 10:55
Show Gist options
  • Save nextab/1f214bbd119bdb641ae8bc8ae74345e6 to your computer and use it in GitHub Desktop.
Save nextab/1f214bbd119bdb641ae8bc8ae74345e6 to your computer and use it in GitHub Desktop.
Snippet for the functions.php of your WordPress (child) theme to allow .svg as a file format.
#region Allow .svg files
function nxt_allow_svg($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'nxt_allow_svg');
function nxt_really_allow_svg($checked, $file, $filename, $mimes){
if(!$checked['type']){
$wp_filetype = wp_check_filetype( $filename, $mimes );
$ext = $wp_filetype['ext'];
$type = $wp_filetype['type'];
$proper_filename = $filename;
if($type && 0 === strpos($type, 'image/') && $ext !== 'svg'){
$ext = $type = false;
}
$checked = compact('ext','type','proper_filename');
}
return $checked;
}
add_filter('wp_check_filetype_and_ext', 'nxt_really_allow_svg', 10, 4);
#endregion Allow .svg files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment