Conditionally Allow SVG Uploads

If you care about website performance and easier administration, I recommend using a code snippet plugin like WPCodeBox (or similar) for adding code to your website.

Instructions

  1. Create a new PHP code snippet.
  2. Copy the contents of code snippet below.
  3. Paste the contents into your code snippet.
  4. Review any notes that I’ve provided.
  5. Save and enable the code snippet.
  6. Test.

Snippet

This code snippet allows you to remove plugins that have the sole purpose of allowing SVG uploads.

Using WPCodeBox, you can choose whether to allow SVGs to be uploaded in the Admin area only, on the front end (e.g., Listing Submit Form), or both. You could also use WPCodeBox to let certain roles upload images from the front end.

WPCodeBox is not required but offers much flexibility regarding functionality, security, and performance.

add_filter("upload_mimes", function ($mimes) {
    $mimes["svg"] = "image/svg+xml";
        return $mimes;
    });

add_filter("wp_check_filetype_and_ext", function ($result, $file, $filename, $mimes) {
    if (!$result["ext"] || !$result["type"]) {
        $filetype = wp_check_filetype($filename, $mimes);
        $ext = $filetype["ext"];
        $type = $filetype["type"];
        $allowed_types = [
            "svg" => [
                "image/svg+xml"
            ],
        ];
        if (isset($allowed_types[$ext]) && in_array($type, $allowed_types[$ext])) {
            $result = [
                "ext" => $ext,
                "type" => $type,
                "proper_filename" => $filename
            ];
        }
    }
    return $result;
}, 10, 4);

🔥NEW!🔥
Property Starter Site
MyListing Add-on
Launch Sale Ends: June 24th.

Club Guarantee

If you notice an issue with this product, I’ll fix it for free and get you an updated version.

Club Guarantee

If you notice an issue with a Club Product and the problem exists on the corresponding Starter Site, I’ll fix it for free.