AI Verified

Name

Allow SVG

About

Allow SVG upload without a plugin

Language

PHP

Rating

Voted: 4 by 4 user(s)

How to Setup Snippet

pste into your functions php

Codevault

Erling

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: Not Specified

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

04/07/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

-------------------------------------------------------------------------------------------------------------
Name | Potential vulnerability found : File Inclusion / Path Traversal
Line | 20
Code | file_get_contents($path)
Code | Line | 18 : $path = get_attached_file($attachment->ID));

Name | Potential vulnerability found : XML External Entity
Line | 20
Code | SimpleXMLElement(@file_get_contents($path)
Code | Line | 18 : $path = get_attached_file($attachment->ID));

Found 2 vulnerabilities

Allow SVG

 
                    
1//* allow SVG
2 function def_allow_svgimg_types($mimes) {
3 $mimes['svg'] = 'image/svg+xml';
4 return $mimes;
5 }
6 add_filter('upload_mimes', 'def_allow_svgimg_types');
7 add_filter( 'wp_check_filetype_and_ext', function($def_svg_filetype_ext_data, $file, $filename, $mimes) {
8 if ( substr($filename, -4) === '.svg' ) {
9 $def_svg_filetype_ext_data['ext'] = 'svg';
10 $def_svg_filetype_ext_data['type'] = 'image/svg+xml';
11 }
12 return $def_svg_filetype_ext_data;
13 }, 100, 4 );
14 function def_common_svg_media_thumbnails($response, $attachment, $meta){
15 if($response['type'] === 'image' && $response['subtype'] === 'svg+xml' && class_exists('SimpleXMLElement'))
16 {
17 try {
18 $path = get_attached_file($attachment->ID);
19 if(@file_exists($path))
20 {
21 $svg = new SimpleXMLElement(@file_get_contents($path));
22 $src = $response['url'];
23 $width = (int) $svg['width'];
24 $height = (int) $svg['height'];
25 //media gallery
26 $response['image'] = compact( 'src', 'width', 'height' );
27 $response['thumb'] = compact( 'src', 'width', 'height' );
28 //media single
29 $response['sizes']['full'] = array(
30 'height' => $height,
31 'width' => $width,
32 'url' => $src,
33 'orientation' => $height > $width ? 'portrait' : 'landscape',
34 );
35 }
36 }
37 catch(Exception $e){}
38 }
39 return $response;
40 }
41 add_filter('wp_prepare_attachment_for_js', 'def_common_svg_media_thumbnails', 10, 3);
42//* End allow SVG

4

Related Snippets

Please see some snippets below related to this snippet..

General

AI Verified

1

Remove CTP name from URL slug

Added: 1 year ago

Last Updated: 1 year ago

Code removes the Custom Post Type (CPT) name from the URL slug.

General

AI Verified

0

Ensure Webfont is Loaded

Added: 9 months ago

Last Updated: 9 months ago

General

AI Verified

1

PHP: Disable Wordpress Emojis

Added: 9 months ago

Last Updated: 9 months ago

<p>Disable Wordpress emojis to improve site performance, taken from: https://kinsta.com/knowledgebase/disable-emojis-wordpress/#disable-emojis-code</p>

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

4

Allow SVG

Added: 1 year ago

Last Updated: 3 days ago

Allow SVG upload without a plugin