SVG (Scalable Vector Graphics) is a versatile and scalable image format that’s perfect for web use. Unfortunately, WordPress doesn’t natively support SVG uploads due to security concerns. However, you can enable SVG support in WordPress without using a plugin by adding some custom code to your theme. Here’s how to do it safely:
Why Use SVGs?
SVG images offer several advantages over other image formats:
Scalability: SVGs are vector-based, meaning they can scale to any size without losing quality.
Smaller File Size: SVG files are often smaller than bitmap images, improving load times and performance.
Editability: SVGs can be edited with text editors, making them easy to customize.
Step 1: Enable SVG Uploads
To enable SVG uploads, you need to add a function to your theme’s functions.php file. Follow these steps:
Step 1.1: Access Your Theme’s functions.php File
1. Log in to your WordPress dashboard.
2. Navigate to Appearance > Theme Editor.
3. In the right sidebar, find and click on the functions.php file of your active theme.
Step 1.2: Add the SVG Upload Function
Add the following code to the functions.php file to allow SVG uploads:
function cc_mime_types($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');
Save the changes to your functions.php file.
Step 2: Secure SVG Uploads
Since SVG files can potentially contain malicious code, it’s crucial to secure SVG uploads by sanitizing them. Add the following code to your functions.php file:
function sanitize_svg($svg) {
$svg = simplexml_load_string($svg, 'SimpleXMLElement', LIBXML_NOERROR | LIBXML_NOWARNING);
if (!$svg) {
return '';
}
$allowed_tags = array('svg', 'g', 'path', 'rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'text', 'tspan', 'tref', 'textPath', 'image', 'use', 'defs', 'title', 'desc', 'metadata');
$svg = remove_disallowed_tags($svg, $allowed_tags);
return $svg->asXML();
}
function remove_disallowed_tags($svg, $allowed_tags) {
foreach ($svg->children() as $child) {
if (!in_array($child->getName(), $allowed_tags)) {
unset($svg->$child);
} else {
$svg->$child = remove_disallowed_tags($child, $allowed_tags);
}
}
return $svg;
}
function svg_sanitizer($file) {
if ($file['type'] === 'image/svg+xml') {
$file['tmp_name'] = sanitize_svg(file_get_contents($file['tmp_name']));
}
return $file;
}
add_filter('wp_handle_upload_prefilter', 'svg_sanitizer');
Step 3: Test SVG Uploads
1. Navigate to Media > Add New in your WordPress dashboard.
2. Try uploading an SVG file to ensure that the upload works correctly and the file is displayed properly.
Benefits of Enabling SVG Support
Enabling SVG support in WordPress allows you to:
Use Scalable Graphics: Ensure that images look crisp and clear at any size.
Improve Site Performance: Reduce image file sizes for faster load times.
Enhance Design Flexibility: Easily customize and edit vector graphics.
Conclusion
By following these steps, you can safely enable SVG support in WordPress without using a plugin. This allows you to take advantage of the scalability and flexibility of SVG images while ensuring your site remains secure. Remember to always sanitize SVG files to prevent potential security issues.

Post a Comment