Just like .webp files, by default, we cannot upload SVG files in WordPress because of built-in filters that are active to minimize the upload of files that could harm our website.
When we have not added a specific filter in WordPress to allow SVG file uploads, we will get an error message that says “Sorry, you are not allowed to upload this file type.” and we cannot even select the SVG file from our computer.
So, how do we upload SVG files in WordPress and be able to select SVG files directly from our computer?
How to Upload SVG Files in WordPress
To be able to upload SVG files in WordPress, we need to add a filter to our WordPress site, either manually or by using a plugin.
However, for a simpler way, it is recommended to use the method of allowing SVG uploads using a plugin.
1. Manually Allow SVG File Uploads
This manual method is recommended if you are using a child theme in WordPress. If you are using the main theme directly, it is recommended to allow uploads using a plugin as described in the second method.
To manually allow SVG file uploads, first we need to open the WordPress dashboard with admin access, then open the menu Appearance → Theme File Editor.
Make sure the active theme you are using is not the Twenty Twenty-Two theme or a newer default theme version, because those themes do not support the Theme File Editor feature.
After you are on the Theme File Editor page, open the menu or option Functions in that theme so we can add a line of code to allow the website owner to upload SVG files.
Are you on the theme functions page? Now we just need to add the code below to the very last line of the functions file, then save the changes.
/** * Allow SVG upload di WordPress. */add_filter( 'upload_mimes', function ( $upload_mimes ) { // Jika ingin allow upload SVG untuk user dengan role tertentu, // silahkan menghapus komentar pada tiga baris kode dibawah ini /** if ( ! current_user_can( 'administrator' ) ) { return $upload_mimes; } */ // END User Role. $upload_mimes['svg'] = 'image/svg+xml'; $upload_mimes['svgz'] = 'image/svg+xml';
return $upload_mimes; });
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);/** * End Allow SVG upload di WordPress. */After adding the line of code above to the active theme functions file and saving it, we can then upload SVG files in WordPress as long as that code remains in the theme and is not deleted.
2. Allow SVG File Uploads Using a Plugin
If you want to use a plugin, we can use a WordPress plugin called WPCode – Insert Headers and Footers + Custom Code Snippets – WordPress Code Manager and then activate it on our website.
Then, to start enabling the SVG upload feature, we can add a new snippet by opening the menu Code Snippets → Add New Snippet.
Here we can choose to create a new snippet or use the existing Allow SVG Files Upload snippet.
If you choose the existing Allow SVG Files Upload snippet, we can immediately activate the snippet without adding more code, but the provided snippet is code to allow SVG uploads only for users with the Administrator role.
So, if we want other users to be able to upload SVG files, we need to modify that snippet code.
For example, if we want all users to be able to upload SVG files by default, we can choose the Custom Snippet option, then add the following line of code
/** * Allow SVG upload di WordPress. */add_filter( 'upload_mimes', function ( $upload_mimes ) { // Jika ingin allow upload SVG untuk user dengan role tertentu, // silahkan menghapus komentar pada tiga baris kode dibawah ini /** if ( ! current_user_can( 'administrator' ) ) { return $upload_mimes; } */ // END User Role. $upload_mimes['svg'] = 'image/svg+xml'; $upload_mimes['svgz'] = 'image/svg+xml';
return $upload_mimes; });
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);/** * End Allow SVG upload di WordPress. */After the line of code is added, make sure to give the code snippet an easy-to-remember name, change the snippet type to PHP and then activate the code snippet.
If the code snippet has been added, saved, and activated, we can then try uploading a new SVG file to our website, and now the error message “Sorry, you are not allowed to upload this file type.” will no longer appear.
Conclusion
Although there are two different ways to upload SVG files to WordPress, using a plugin is more recommended than the manual method because it is safer and easier.
This is because if we use the manual method in a theme while not using a child theme, the filter will be lost when we update the theme.
COMMENTS
Loading...