{"id":13199,"date":"2023-10-18T07:20:21","date_gmt":"2023-10-18T01:50:21","guid":{"rendered":"https:\/\/codexcoach.com\/?p=13199"},"modified":"2024-07-02T10:26:10","modified_gmt":"2024-07-02T04:56:10","slug":"enabling-svg-support-in-wordpress-programmatically","status":"publish","type":"post","link":"https:\/\/codexcoach.com\/enabling-svg-support-in-wordpress-programmatically\/","title":{"rendered":"Enabling SVG Support in WordPress Programmatically"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>SVG (Scalable Vector Graphics) is a format that has been gaining attention due to its scalability and responsiveness. <a href=\"https:\/\/codexcoach.com\/category\/wordpress\/\">WordPress<\/a>, by default, does not support SVG file uploads for security reasons. However, there are ways to enable SVG support programmatically for your WordPress website. This blog will guide you through the steps to achieve that, thereby allowing for greater customization and flexibility.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Methods to Enable SVG Support<\/h2>\n\n\n\n<p>There are various methods to enable SVG support, but we&#8217;ll focus on doing this programmatically using PHP. You can add this code to your theme\u2019s <code>functions.php<\/code> file or install a simple plugin.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Method 1: Insert a Code Snippet<\/strong><\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\nadd_filter( &#39;wp_check_filetype_and_ext&#39;, function($data, $file, $filename, $mimes) {\n\n\tglobal $wp_version;\n\tif ( $wp_version !== &#39;4.7.1&#39; ) {\n\t\treturn $data;\n\t}\n\n\t$filetype = wp_check_filetype( $filename, $mimes );\n\n\treturn [\n\t\t&#39;ext&#39;             =&gt; $filetype[&#39;ext&#39;],\n\t\t&#39;type&#39;            =&gt; $filetype[&#39;type&#39;],\n\t\t&#39;proper_filename&#39; =&gt; $data[&#39;proper_filename&#39;]\n\t];\n\n}, 10, 4 );\n\nfunction cxc_mime_types( $mimes ){\n\t$mimes[&#39;svg&#39;] = &#39;image\/svg+xml&#39;;\n\treturn $mimes;\n}\nadd_filter( &#39;upload_mimes&#39;, &#39;cxc_mime_types&#39; );\n\nfunction cxc_fix_svg() {\n\techo &#39;&lt;style type=&quot;text\/css&quot;&gt;\n\t.attachment-266x266, .thumbnail img {\n\t\twidth: 100% !important;\n\t\theight: auto !important;\n\t}\n\t&lt;\/style&gt;&#39;;\n}\nadd_action( &#39;admin_head&#39;, &#39;cxc_fix_svg&#39; );\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"functions.php\" data-lang=\"PHP\"><code>&lt;?php\nadd_filter( &#39;upload_mimes&#39;, &#39;cxc_file_types_to_uploads&#39; );\nfunction cxc_file_types_to_uploads( $file_types ){\n\t$cxc_filetypes = array();\n\t$cxc_filetypes[&#39;svg&#39;] = &#39;image\/svg+xml&#39;;\n\t$file_types = array_merge($file_types, $cxc_filetypes );\n\treturn $file_types;\n}\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<p><strong>Also read:<\/strong> <strong><a href=\"https:\/\/codexcoach.com\/how-disable-zoom-lightbox-and-gallery-slider-on-woocommerce-single-product\/\">How Disable Zoom, Lightbox, And Gallery Slider On WooCommerce Single Product<\/a><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Method 2: Make Use of a Plugin<\/strong><\/h3>\n\n\n\n<p>While adding SVG support programmatically to your WordPress website offers a lot of control, using a plugin can simplify the process, particularly for those who are less familiar with coding. Plugins often come with added features like SVG sanitization, which enhances the security of your uploads.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Installation<\/h3>\n\n\n\n<p>First, download and set up a plugin called <a href=\"https:\/\/wordpress.org\/plugins\/svg-support\/\" rel=\"nofollow noopener\" target=\"_blank\">SVG Support<\/a>.<\/p>\n\n\n\n<p>After it&#8217;s on, go to your WordPress settings. You&#8217;ll see a new option called SVG Support. Click on it to learn how to make your SVG files look good on your website.<\/p>\n\n\n\n<p>You can also make it so only the website&#8217;s admins can upload SVG files.<\/p>\n\n\n\n<p>Once you do all this, you can start adding SVG files to your website. But make sure to check for other important stuff first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Turn On GZip for SVGs on Your Server <\/h3>\n\n\n\n<p>The way to do this depends on who&#8217;s hosting your website and how your server is set up. Some hosts, like WP Engine, already use GZip for certain files but not for SVGs.<\/p>\n\n\n\n<p>To make sure your SVGs load fast, you need to add this type of file to your website\u2019s list. Do this in your .htaccess file on your server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Make Sure the Plugin Keeps Files Safe <\/h3>\n\n\n\n<p>SVG files can have some security risks, which is why WordPress doesn&#8217;t support them by default. These files can be open to certain types of attacks because they&#8217;re based on XML.<\/p>\n\n\n\n<p>When you set up your SVG plugin, it&#8217;s a good idea to let only admins upload SVGs. For extra safety, you can &#8216;clean&#8217; the SVG files before you upload them to get rid of any risky code.<\/p>\n\n\n\n<p>While the SVG Support plugin doesn&#8217;t clean the files for you, some plugins do. One example is Safe SVG.<\/p>\n\n\n\n<p>You can also use your own tool to clean SVG files. The code for the Safe SVG cleaning tool is free for anyone to use and can be found on GitHub.<\/p>\n\n\n\n<p>Having your own cleaning tool is smart if you&#8217;re going to use SVGs on your WordPress site.<\/p>\n<div id=\"ezoic-pub-ad-placeholder-118\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Introduction SVG (Scalable Vector Graphics) is a format that has been gaining attention due to its scalability and responsiveness. WordPress, by default, does not support SVG file uploads for security reasons. However, there are ways to enable SVG support programmatically for your WordPress website. This blog will guide you through the steps to achieve that, [&#8230;]<\/p>\n","protected":false},"author":8,"featured_media":13202,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_aib_schema_json_ld":""},"categories":[10],"tags":[],"_links":{"self":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/13199"}],"collection":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/comments?post=13199"}],"version-history":[{"count":1,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/13199\/revisions"}],"predecessor-version":[{"id":13201,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/13199\/revisions\/13201"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media\/13202"}],"wp:attachment":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media?parent=13199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/categories?post=13199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/tags?post=13199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}