By default, WordPress blocks SVG uploads due to security risks. SVGs, while scalable and lightweight, can carry malicious code, making them vulnerable to attacks like XSS or XXE. However, you can safely enable SVG uploads using plugins or custom code.
Key Methods to Enable SVG Uploads:
- Safe SVG Plugin: Automatically sanitizes files, ensuring security. Ideal for beginners or multi-user sites.
- SVG Support Plugin: Offers advanced features like inline rendering and CSS styling. Best for designers needing customization.
- Custom Code: Adds SVG support manually via
functions.php. Suitable for developers but requires careful sanitization.
Security Tips:
- Restrict uploads to trusted users (e.g., Administrators).
- Use trusted sources for SVG files.
- Sanitize files to remove harmful scripts.
Choose a method based on your technical skills and site needs. Plugins are safer for most users, while custom code offers flexibility for experienced developers.
SVG Security Risks and How to Prevent Them
Security Risks of SVG Files
SVG files, while incredibly versatile, come with their own set of security challenges. Since they’re XML-based, they can embed malicious JavaScript, which opens the door to cross-site scripting (XSS) attacks. When someone views an infected SVG in their browser, the embedded scripts could steal session cookies, capture login details, or even redirect unsuspecting users to phishing websites.
"SVGs are not raster images like other formats… they are actually vectors and because of that they can potentially be used to carry malicious JavaScript." – Simon Codrington, Author
Another major concern is XML External Entity (XXE) attacks, which exploit vulnerabilities in XML processing. These attacks can allow hackers to access sensitive server files or execute unauthorized requests. For example, a compromised SVG could potentially grant attackers access to your WordPress dashboard or server environment.
Recognizing these risks is the first step toward implementing effective security measures.
How to Use SVG Files Safely
The cornerstone of safe SVG use is sanitization. Sanitizing SVG files removes harmful scripts and external links that could pose security threats. Plugins like Safe SVG can automate this process by using sanitization libraries, making them a safer option than manually adding SVG upload functionality to your functions.php file without additional safeguards.
Here are a few additional tips for secure SVG usage:
- Restrict upload permissions: Only allow trusted users, such as Administrators, to upload SVG files. This minimizes the risk of accidental or malicious uploads by Contributors or Authors.
- Stick to trusted sources: Avoid downloading random SVGs from search engines. These files could be hiding malicious code. Instead, rely on trusted sources or create your own SVGs when possible.
- Optimize and clean your SVGs: Tools like SVGOMG can help you minify SVG code and strip out unnecessary metadata or hidden layers. This not only reduces file size but also shrinks the potential attack surface.
Method 1: Using the Safe SVG Plugin

Installing and Activating Safe SVG
The Safe SVG plugin by 10up is a popular choice for securely uploading SVG files to WordPress. With over 1 million active installations and an impressive 4.9/5 star rating on WordPress.org, it’s a trusted solution for many website owners.
To get started, log in to your WordPress admin dashboard and head to Plugins > Add New. Search for "Safe SVG" and look for the plugin developed by 10up. Once you find it, click Install Now and then Activate. The plugin gets to work immediately, sanitizing any SVG files you upload to safeguard against XML/SVG vulnerabilities. After installation, you can tweak the plugin settings to control who can upload SVG files.
A great bonus of this plugin is its ability to display SVG previews in the Media Library. Without it, WordPress shows blank icons for SVG files, making them tricky to identify. With Safe SVG, you’ll see actual thumbnails for your vector graphics, just like you do for JPG or PNG files.
Configuring Safe SVG Settings
To fine-tune the plugin, go to Settings > Media and scroll to the bottom of the page. For security, it’s best to restrict SVG uploads to Administrators – or Editors, if they are fully trusted. This step helps keep your site in line with WordPress security best practices.
Another handy feature is the optional SVGO optimization, which can shrink SVG file sizes. While this feature is turned off by default, you can enable it by adding this code snippet to your theme’s functions.php file:
add_filter( 'safe_svg_optimizer_enabled', '__return_true' );
This optimization can make a big difference since SVG files are often up to 90% smaller than their JPG or PNG counterparts.
If you frequently work with large vector files (10MB or more), you’ll be glad to know that Safe SVG version 2.3.0 and later handles these files effectively. Just select your preferred user roles in the plugin’s settings and click Save Changes to finalize your setup.
Method 2: Using the SVG Support Plugin

Installing and Activating SVG Support
The SVG Support plugin is a popular choice for enabling vector graphics in WordPress. With over 1 million active installations and a 4.8/5 star rating, it’s trusted by many users. To get started, head to Plugins > Add New in your WordPress dashboard, search for "SVG Support", and click Install Now. Once the installation is complete, hit Activate.
After activation, you can upload SVG files directly to your Media Library. The plugin automatically sanitizes these uploads to remove potentially harmful code. For most users, the default settings are sufficient, but there are advanced options available for those who need more control.
Configuring Advanced Features
Beyond secure uploads, the plugin offers advanced settings to enhance how SVGs function on your site. Navigate to Settings > SVG Support and enable Advanced Mode to unlock additional features like inline SVG rendering, minification, and CSS styling.
Inline SVG rendering replaces the standard <img> tag with the actual SVG code. This allows you to style or manipulate individual elements using CSS or JavaScript. To use this feature in the Block Editor, simply add the CSS class style-svg to your Image or Cover block. If your page builder doesn’t allow easy addition of custom classes, enabling the Force Inline SVG option will ensure all SVG files render inline automatically.
The plugin also helps boost performance by minifying SVG files. It removes unnecessary metadata during uploads, which can reduce file sizes and improve loading times.
Restricting SVG Uploads by User Role
To keep your site secure, you can restrict SVG uploads to specific user roles. In the "Restrict SVG Uploads to" dropdown, select "Administrator" to limit this capability to trusted users only. This aligns with best practices for managing file uploads.
There’s also an option labeled "Do not Sanitize for These Roles." This allows certain roles to bypass the plugin’s automatic code cleaning. For security reasons, it’s recommended to leave this blank or restrict it to Administrators only, ensuring all uploaded files are sanitized. Once you’ve made your adjustments, click Save Changes to apply the settings.
How To Upload SVG Files In WordPress (2 Methods)

sbb-itb-f80d703
Method 3: Adding Custom Code to functions.php
If you’re looking for more control over how SVG uploads work on your WordPress site, adding custom code to your child theme’s functions.php file is an effective option.
How to Edit the functions.php File
Before making any changes, always back up your functions.php file. You can edit this file directly from your WordPress dashboard by navigating to Appearance > Theme File Editor.
It’s crucial to make these changes in your child theme’s functions.php file, not the parent theme. Why? Because updates to the parent theme will erase your customizations. If you don’t have a child theme yet, create one to keep your changes safe.
For added precaution, enable WP_DEBUG in your wp-config.php file while editing. This will help you catch any syntax errors immediately.
Code to Enable SVG Uploads
This method gives you more flexibility compared to plugins, but it requires careful handling. The primary step involves using the upload_mimes filter to let WordPress recognize .svg files. However, because SVGs are XML-based, they can include harmful JavaScript. To mitigate this, use DOMDocument to sanitize the SVG by removing <script> elements.
To enhance security, restrict SVG uploads to administrators only. Limiting access ensures lower-level users, like Editors or Contributors, can’t upload potentially unsafe files.
One common hiccup with this approach is broken SVG thumbnails in the Media Library. To fix this, you can add a small CSS snippet to the admin_head hook. This snippet adjusts the thumbnail display by setting the width to 100% and the height to auto.
Once you’ve added the code, test the functionality by uploading an SVG file.
Testing and Troubleshooting
After inserting the custom code, head to Media > Add New and try uploading an SVG file. If you encounter the error, “Sorry, this file type is not permitted for security reasons,” it usually means the file’s content doesn’t match the required image/svg+xml MIME type. Check that your SVG starts with a valid XML declaration, such as:
<?xml version="1.0" encoding="utf-8"?>.
To ensure your restrictions work, log in as a lower-level user (like an Editor) and confirm they can’t upload SVG files. Then, log in as an Administrator, upload an SVG, and insert it into a post or page. Test the SVG’s display across different browsers, including Chrome, Firefox, Safari, and Edge, to confirm compatibility.
If you encounter a blank screen error, use FTP to revert your changes and troubleshoot further.
Plugins vs Custom Code: Which Method to Choose

WordPress SVG Upload Methods Comparison: Plugins vs Custom Code
Deciding between using a plugin or custom code for handling SVG files often boils down to your technical expertise and security priorities. For most users, plugins like Safe SVG are the go-to option because they automatically sanitize files, removing any malicious code. With over 900,000 active installations and a 5-star rating, Safe SVG has earned its reputation as a reliable choice.
"For most users, installing a plugin like Safe SVG is the best and safest option, especially when dealing with a live production site."
– Param Chahal, Founder, TemplateToaster
Custom code, on the other hand, provides more control but comes with risks. It only activates the MIME type for SVG without any built-in sanitization unless you write and maintain complex logic yourself. This makes it less secure for most scenarios. However, if you’re a developer working on a single-user site and creating your SVG files using trusted tools like Adobe Illustrator, custom code can work – provided you’re prepared to manually sanitize files and keep your code secure and up to date.
Comparison Table
| Feature | Safe SVG Plugin | SVG Support Plugin | Custom Code |
|---|---|---|---|
| Ease of Use | High; no coding required | Medium; requires setup | Low; needs PHP skills |
| Security | High; automatic sanitization | High; automatic sanitization | Low; manual sanitization required |
| Maintenance | Easy; plugin updates | Easy; plugin updates | Moderate; child theme needed |
| User Restrictions | Pro version only | Included in free version | Not included |
| Advanced Options | Minimal | Inline rendering, CSS/JS support | None unless custom-built |
| Performance | Slight plugin overhead | Slight plugin overhead | Lightweight; no extra files |
For sites with multiple contributors or heightened security requirements, plugins are the safer bet. Their automatic sanitization and role-based restrictions justify the small trade-off in performance. Conversely, custom code is better suited for experienced developers who are confident in managing security manually and working within controlled environments.
Conclusion
Adding SVG upload functionality to your WordPress site can be straightforward if you stick to secure practices. Each method offers a different balance of ease and security, so it’s important to choose the one that suits your skill level and site requirements.
Recap of Methods and Security Tips
- Safe SVG: This plugin is a great choice for beginners and multi-user sites because it automatically sanitizes uploaded SVG files, reducing potential risks.
- SVG Support: Ideal for designers, this option allows for advanced features like inline rendering and CSS styling, offering more creative flexibility.
- Custom Code: A lightweight solution for experienced developers who are comfortable managing security manually.
The right method depends on your technical know-how and security priorities. If you want a simple, hands-off solution, go with Safe SVG. Designers looking for more control over styling might prefer SVG Support. Developers who are confident in their coding skills can implement custom solutions, but they must ensure files are properly sanitized.
Regardless of the method, always limit SVG upload permissions to trusted user roles, such as Administrators. As WP Society warns, "While SVG files are generally safe, they can pose security risks if not properly sanitized. Use trusted plugins and limit user upload permissions to mitigate these risks". Avoid uploading SVGs from untrusted sources, as they might contain harmful code.
For those using custom code, it’s wise to run every SVG file through an external sanitizer, like SVG Sanitizer Test, before uploading. By combining these precautions with restricted user roles, you can safely take advantage of SVG’s benefits, such as smaller file sizes and enhanced design capabilities.
FAQs
Why can’t I upload SVG files to WordPress by default?
WordPress blocks SVG uploads by default because SVG files are XML-based and can contain malicious code, like scripts that could lead to cross-site scripting (XSS) attacks. This makes them a potential security threat to your site and its visitors.
If you want to allow SVG uploads safely, you can use plugins or implement extra security steps to sanitize the files before they’re uploaded.
How can I safely enable SVG uploads in WordPress?
To safely allow SVG uploads in WordPress, start by installing a reliable SVG sanitizing plugin like Safe SVG or SVG Support. These plugins are designed to automatically clean uploaded files by removing harmful elements, such as embedded JavaScript.
Next, limit SVG upload permissions to specific user roles, like Administrators or Editors, to minimize the risk of unauthorized uploads. It’s also crucial to keep your WordPress core and plugins updated regularly to ensure you’re protected by the latest security fixes. For an extra layer of caution, you can manually review SVG files before uploading or test them in a staging environment to confirm they function correctly without posing any threats.
By taking these precautions, you can safely enable SVG uploads while maintaining the security of your WordPress site.
What’s the difference between using a plugin or custom code to enable SVG uploads in WordPress?
Both plugins and custom code can enable SVG uploads in WordPress, but they come with different trade-offs in terms of usability, security, and upkeep.
Plugins are the go-to choice for ease of use. They often include built-in SVG sanitization, which helps protect your site from malicious scripts. Plus, plugins stay active even after theme updates, so you don’t have to reconfigure anything. The downside? They add a bit of performance overhead and need regular updates to avoid compatibility issues.
Custom code, on the other hand, is a lightweight solution. By adding a snippet to your functions.php file or a site-specific plugin, you can enable SVG uploads without relying on extra plugins. However, this approach doesn’t automatically sanitize SVG files, so you’ll need to handle security checks yourself. If you add the code directly to your theme, it could get wiped out during theme updates – unless you’re using a child theme.
In short, plugins make things simple and secure, while custom code offers a streamlined option that demands more technical know-how and attention to security.
