Although SVG has many advantages such as small size, high resolution, and good scalability, WordPress does not allow SVG file uploads by default due to security and compatibility reasons. If you want to use SVG in WordPress, you need to make some adjustments in the source code or use a plugin to allow these files to be uploaded safely.
Next, I will guide you how to Upload SVG Files to WordPress Latest Update
How to Upload SVG Files to WordPress
To insert the code into WordPress and make sure it works, you can follow these steps:
Step 1: Log in to WordPress Admin
Access your WordPress admin dashboard using your login credentials.
Step 2: Edit the functions.php file
-
- Go to Appearance: In the left panel, select Appearance and then select Editor.
- Find the
functions.php
file: In the list of files on the right, find and click the file namedfunctions.php
. This is the file where you'll add the code.
Step 3: Add the code
- Scroll down to the bottom of the
functions.php
file. - Paste the code below at the end of the file:
See more.
- Code to automatically save images to hot when copying images from other pages
- Code to display hotline on website
- Share beautiful related article code for Flatsome
function howto_edu_vn_mime_types($mimes) { $mimes['svg'] = $mimes['svgz'] = 'image/svg+xml'; return $mimes;} add_filter('upload_mimes', 'howto_edu_vn_mime_types'); function howto_edu_vn_mimetype_svg($data, $file, $filename, $mimes) { $extension = pathinfo($filename, PATHINFO_EXTENSION); if (in_array($extension, ['svg', 'svgz'])) { $data['type'] = 'image/svg+xml'; $data['ext'] = $extension;} return $data;} add_filter('wp_check_filetype_and_ext', 'howto_edu_vn_mimetype_svg', 100, 4);
Step 4: Save changes
- Click the Update File button to save the changes you have made.
Step 5: Test the operation
- Go to the Media section in your WordPress dashboard.
- Click the Add New button to upload an SVG or SVGZ file.
- Check if the file is visible or if the file was uploaded successfully.
Note
- Before making any changes to the
functions.php
file, you should back up this file or create a full backup of your website to avoid data loss if something goes wrong. - If you don't see a
functions.php
file, make sure you're using a child theme or a parent theme that supports editing.
Meaning of the code:
Jaw howto_edu_vn_mime_types($mimes)
:
- This function takes an array
$mimes
containing previously defined file types. - It adds two new file types to the array:
svg
andsvgz
with the MIME type value ofimage/svg+xml
. - Finally, the function returns the updated array
$mimes
add_filter('upload_mimes', 'howto_edu_vn_mime_types')
- This line uses WordPress's add_filter function to connect the howto_edu_vn_mime_types function to the upload_mimes hook.
- This allows WordPress to recognize files with the
svg
andsvgz
extensions when users upload them.
Jaw howto_edu_vn_mimetype_svg($data, $file, $filename, $mimes)
- This function checks the file type and file extension when a file is uploaded.
- It uses the
pathinfo
function to get the extension of the file name. - If the extension is
svg
orsvgz
, it updates$data
to specify the MIME type asimage/svg+xml
and saves the extension to$data['ext']
.
add_filter('wp_check_filetype_and_ext', 'howto_edu_vn_mimetype_svg', 100, 4)
- This line connects the function
howto_edu_vn_mimetype_svg
to the hookwp_check_filetype_and_ext
. - The parameter
100
specifies the priority of the filter, and4
indicates the number of parameters the function can take.
There are a few main reasons why WordPress doesn't allow SVG (Scalable Vector Graphics) file uploads by default:
1. Security issues:
- Malicious code: This way of uploading SVG files to WordPress if you don't know where the file is from, it is easy to get malicious code inserted. SVG is an XML format, which can contain JavaScript code. If an SVG file is tampered with or contains malicious code, it can be executed on your website, leading to security issues such as XSS (Cross-Site Scripting) attacks.
- User Risk: If SVG file uploads are allowed without checking, users may upload files containing malicious code, which could compromise your website and data.
2. Incompatible with browsers:
- While most modern browsers support SVG, some older browsers may not fully support it, leading to display issues.
3. File control:
- WordPress wants to control the types of files users can upload to ensure that only safe and supported files can be used on the system.
4. Lack of error handling:
- If an SVG file is not created properly, it can cause errors or display incorrectly. WordPress needs to control file types for stability and compatibility.
Conclude
I have just shared with you the latest how to upload SVG files to WordPress. If you want to use SVG files in your website, you need to take necessary measures to ensure safety, such as using a supporting plugin or adjusting the source code to allow safe SVG uploading. This will help you take advantage of the advantages of the SVG format while still maintaining the security of the website.
Also, don't forget to follow our Fanpage facebook Howto.edu.vn channel to get the latest updates.