Inline SVG icons keep WordPress navigation bars and feature grids razor-sharp without extra font files, yet raw vector exports from design tools often carry bloated metadata—or dangerous script vectors if pasted from unverified sources. The CodeToWP Sanitized SVG Icon Cleaner for WordPress cleans SVG markup in the browser and builds a ready-to-use codetowp_get_icon() helper backed by WordPress’s wp_kses() whitelist.
Who the Sanitized SVG Icon Cleaner for WordPress Is Built For
Design tools like Figma, Illustrator, and Sketch export XML declarations, doctypes, comments, and <metadata> blocks that bloat HTML payloads. Even riskier, unvetted SVG snippets can embed <script> tags, javascript: href URIs, or inline event handlers such as onload and onerror. Furthermore, outputting dynamic SVGs in WordPress templates requires an explicit wp_kses() tag whitelist because standard wp_kses_post() strips SVG elements. Using our Sanitized SVG Icon Cleaner for WordPress keeps theme icon helpers fast and XSS-safe.
Features, Settings & Supported Outputs
- Presets & File Upload: Test with 3 built-in samples (including an XSS-tainted Heroicons Bell test icon, Lucide Shield, and Feather Arrow), drag-and-drop or upload
.svg/.txtfiles, or paste raw markup. - 4 Cleanup & Normalization Toggles: Independently toggle Normalize to
currentColor(replaces non-none/non-transparentfillandstrokevalues), Strip XML Declaration & Metadata (<?xml?>,<!DOCTYPE>,<metadata>,<!-- comments -->), Strip Script & Event Attributes (<script>,on*handlers,javascript:URIs), and blank-line trimming. - Two Live Outputs & Render Preview: Generates sanitized SVG markup (downloads as
icon-sanitized.svg) alongside a completeinc-svg-icons.phphelper definingcodetowp_get_icon( $slug = 'custom-icon', $class = '' )with an allowed-tags array coveringsvg,path,circle,rect,line,polyline, andpolygon, plus a sandboxed Live Render preview.
Practical Example: Cleaning an XSS-Tainted SVG Icon
Sample Raw SVG Input
<?xml version="1.0" encoding="UTF-8"?>
<!-- Exported Icon -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="#1e293b" stroke-width="2" onload="alert(1)">
<script>alert('xss')</script>
<path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
</svg>
Expected Cleaned Output & PHP Helper Extract
// Sanitized SVG:
// <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" >
// <path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/>
// </svg>
// Generated inc-svg-icons.php usage:
echo codetowp_get_icon( 'custom-icon', 'w-5 h-5 text-primary' );
Result Explanation: The XML declaration, comment, <script> payload, and onload attribute are stripped cleanly, stroke="#1e293b" converts to currentColor so CSS classes style the icon dynamically, and codetowp_get_icon() escapes output via wp_kses().
Practical Use Cases
- WordPress Theme Developers: Replace heavy icon font libraries with lightweight inline vectors rendered via
codetowp_get_icon(). - Frontend Developers: Convert fixed hex icon strokes from Figma into CSS-inherited
currentColoricons. - Agencies: Standardize safe UI icon rendering inside custom headers, menus, and buttons with strict
wp_kses()sanitization. - Freelancers: Strip bloated editor namespaces from client-supplied vector icons before embedding them in custom themes or Elementor widgets.
Limitations & Compatibility
- Regex Sanitization Scope (Not a Full XML DOM Sanitizer): The browser cleaner removes XML declarations, doctypes, metadata, comments,
<script>tags, quotedon*attributes, andjavascript:URIs via regex. Do not rely on client-side regex alone to sanitize untrusted user uploads—advanced SVG payloads (<foreignObject>,<use>,<style>,data:URIs, or unquoted handlers) require server-side DOM/XML sanitization if handling untrusted user submissions. - Allowed-Tags Whitelist Covers Core Icon Primitives: The generated
$allowed_tagsPHP array permitssvg,path,circle,rect,line,polyline, andpolygon. Grouped icons or complex illustrations using<g>,<defs>, gradients,<text>, orfill-rulewill have unlisted elements stripped bywp_kses()unless you add those tags/attributes manually to$allowed_tags. - Helper & Style Edge Cases: Passing
$classtocodetowp_get_icon()insertsclass="..."after<svg(avoid having a pre-existingclass=on the root<svg>or empty<svg>without attributes), andcurrentColornormalization converts multi-color attribute icons to monochrome while ignoring inlinestyle="fill:..."properties. - Compatibility: Generated PHP works cleanly on WordPress 5.0–6.7+ and PHP 8.0–8.3+.
Next Steps & Related CodeToWP Tools
Read our tutorial on Sanitizing Inline SVG Icons in WordPress with wp_kses(), embed cleaned vectors generated by the Sanitized SVG Icon Cleaner for WordPress inside custom blocks built with our Elementor Widget Generator, reinforce theme security using the Security Snippet Generator, or review our WordPress Theme Security Hardening Checklist.