AI Verified

Name

Allow SVG Files Upload

About

Add support for SVG files to be uploaded in WordPress media.

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

Standards

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:

10/10/2023

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:

Found 0 vulnerabilities

Allow SVG Files Upload

 
                    
1/**
2 * Allow SVG uploads for administrator users.
3 *
4 * @param array $upload_mimes Allowed mime types.
5 *
6 * @return mixed
7 */
8add_filter(
9 'upload_mimes',
10 function ( $upload_mimes ) {
11 // By default, only administrator users are allowed to add SVGs.
12 // To enable more user types edit or comment the lines below but beware of
13 // the security risks if you allow any user to upload SVG files.
14 if ( ! current_user_can( 'administrator' ) ) {
15 return $upload_mimes;
16 }
17 
18 $upload_mimes['svg'] = 'image/svg+xml';
19 $upload_mimes['svgz'] = 'image/svg+xml';
20 
21 return $upload_mimes;
22 }
23);
24 
25/**
26 * Add SVG files mime check.
27 *
28 * @param array $wp_check_filetype_and_ext Values for the extension, mime type, and corrected filename.
29 * @param string $file Full path to the file.
30 * @param string $filename The name of the file (may differ from $file due to $file being in a tmp directory).
31 * @param string[] $mimes Array of mime types keyed by their file extension regex.
32 * @param string|false $real_mime The actual mime type or false if the type cannot be determined.
33 */
34add_filter(
35 'wp_check_filetype_and_ext',
36 function ( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) {
37 
38 if ( ! $wp_check_filetype_and_ext['type'] ) {
39 
40 $check_filetype = wp_check_filetype( $filename, $mimes );
41 $ext = $check_filetype['ext'];
42 $type = $check_filetype['type'];
43 $proper_filename = $filename;
44 
45 if ( $type && 0 === strpos( $type, 'image/' ) && 'svg' !== $ext ) {
46 $ext = false;
47 $type = false;
48 }
49 
50 $wp_check_filetype_and_ext = compact( 'ext', 'type', 'proper_filename' );
51 }
52 
53 return $wp_check_filetype_and_ext;
54 
55 },
56 10,
57 5
58);

0

Related Snippets

Please see some snippets below related to this snippet..

General

Unverified

0

WordPress last updated function

Added: 1 year ago

Last Updated: 1 year ago

This is a block of PHP code that can be used to display the date and time when a WordPress post or page was last updated. Here's how it works:

General

AI Verified

1

Deactivate Some Woo Checkout Fields

Added: 8 months ago

Last Updated: 6 months ago

An admin interface for deactivating checkout fields. Sub Menu item can be found in your Woo menu :)

General

AI Verified

0

Change tekst out of stok WooCommerce

Added: 3 weeks ago

Last Updated: 2 weeks ago

Changes tekst out of stok WooCommerce

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

1

Hide Login Errors in WordPress

Added: 9 months ago

Last Updated: 2 weeks ago

General

AI Verified

1

Remove Dashboard Welcome Panel

Added: 9 months ago

Last Updated: 2 weeks ago

Hide the Welcome Panel on the WordPress dashboard for all users.

General

AI Verified

0

Enable Shortcode Execution in Text Widgets

Added: 9 months ago

Last Updated: 9 months ago

Extend the default Text Widget with shortcode execution.