MyListing Club

Conditionally Allow SVG Uploads

Recommendation

If you care about website performance, saving time, reducing administrative overhead, etc., there is no better solution than WPCodeBox when it comes to adding code to your website.

You are not required to use WPCodeBox, but I highly recommend you do.

Learn why I switched to WPCodeBox to manage code snippets for myself and my clients.

Taxonomy

Instructions

IMPORTANT:

I highly recommend testing code snippets in a staging environment before implementing them on your production website.

  1. Create a new PHP snippet in WPCodeBox.
  2. Copy the snippet below.
  3. Paste the contents into WPCodeBox.
  4. Review any notes that I’ve provided.
  5. Save the snippet.
  6. Enable the snippet.
  7. 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 (ex., Listing Submit Form), or both.

Taking it a step further, you could use WPCodeBox to let certain roles upload images from the front end.

WPCodeBox is not required but offers a ton of 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);
				
			
Play Video about mylisting theme tutorial visual studio code walkthrough

Club Partner Deals

Do More. Spend Less.

MyListing Project Template

Save Time. Save Money.
Stop Fumbling Around.

Join the Community

This post may contain affiliate links in accordance with my affiliate policy.

NEW: MyListing Add-on 🔥

Flipping Boxes Listing Counter