How to Safely Add SVG Support to WordPress

Enable SVG uploads in WordPress with security. Learn four methods including sanitization filters and admin-only restrictions.

Why Your WordPress Site Needs SVG

WordPress blocks SVG uploads by default for security reasons, but scalable vector graphics are smaller, sharper on any screen, and perfect for logos, icons, and illustrations. An SVG file that is 50 KB as a PNG becomes just 5 KB as an SVG, cutting image bandwidth by 90 percent. This is especially important for international sites where data costs money. The tradeoff is real: SVG files can contain malicious JavaScript if uploaded carelessly. This guide shows you how to enable SVG safely while protecting your site from injection attacks and maintaining visitor trust.

The Risk and Why It Matters

An attacker could upload an SVG with embedded JavaScript that runs when you view it in WordPress. SVG files are actually XML documents that can contain event handlers and script tags. A malicious SVG can steal admin cookies, redirect visitors, or inject advertising into your site. Sanitizing SVGs requires more than a simple file-type check. You must either use a vetted plugin or write strict filters that remove all potentially dangerous code. The risk is higher if your site allows multiple users to upload media or if you accept user-submitted graphics from your audience.

WordPress core developers decided to block SVG by default because of past security vulnerabilities. In 2015, a researcher demonstrated how malicious SVGs could be used to steal admin credentials through stored XSS (cross-site scripting) attacks. Even in 2024, new SVG attack vectors emerge regularly as browsers add new features. The WordPress security team continues to recommend extreme caution when enabling SVG support, especially on multi-user sites or e-commerce stores.

Common SVG Attack Vectors Explained

Understanding specific attack methods helps you grasp why naive SVG filtering fails. Script tag injection is the most direct: an attacker embeds <script>alert(‘hacked’)</script> directly in the SVG XML. Event handler injection uses onclick, onload, or onerror attributes to trigger JavaScript when a user views the image in certain browsers. A single line like <circle onload=”fetch(‘/admin/steal-data’)”> can steal data when rendered.

Foreign Namespace Injection exploits XML namespaces to hide malicious code in attributes that simple regex filters miss. Data URI schemes can embed JavaScript directly: <image href=”data:text/html,<script>…”>. Animated SVGs with JavaScript triggers can execute code on loop. XSLT processing instructions can transform the SVG into executable code on certain server configurations. Browser-based decompression attacks can use embedded deflate streams to hide payloads from text-based scanners.

This is why a simple regex check for “<script” is insufficient. Professional SVG sanitization requires parsing the full XML tree, understanding namespace contexts, and stripping dangerous elements comprehensively, not just searching for known strings.

Method 1: Use a Whitelist Filter in functions.php

Add this code to your theme’s functions.php file or create a custom plugin:

function allow_svg_upload( $mimes ) {
  $mimes['svg'] = 'image/svg+xml';
  return $mimes;
}
add_filter( 'upload_mimes', 'allow_svg_upload' );

This allows SVG files to upload, but does not sanitize them. Only use this if you are the sole uploader and fully trust the source. This approach is fastest to implement but riskiest. Every time you enable a mime type, you are trusting that the uploader will not accidentally introduce malicious files. This method works best for solo bloggers who manage all media uploads personally.

Be aware that this approach offers zero protection if your hosting account is compromised or if a plugin vulnerability allows unauthorized file uploads. Even if you trust your current users, any future plugin or theme update could introduce a vulnerability that allows an attacker to upload SVGs directly to your server. For this reason, this method should only be used on single-admin sites with strong password policies and regular security audits.

Method 2: Safe SVG Upload with Sanitization

For a more secure setup, add a sanitization filter that strips out scripts and potentially harmful attributes:

function allow_svg_upload_safe( $data, $file, $filename, $mimes ) {
  if ( substr( $filename, -3 ) === 'svg' ) {
    if ( preg_match( '/