X

Restrict uploads to specified file types only, jpg, gif

Adding this snippet to the functions.php of your wordpress theme will allow you to restrict uploads to an array of specified mime types only. This sample only allows jpg and gif images, adding a new line for png eg: 'png' => 'image/png', would then allow png images.


( example screenshot )

Code Snippet:


add_filter('upload_mimes','restrict_mime');
function restrict_mime($mimes) {
$mimes = array(
'jpg|jpeg|jpe' => 'image/jpeg',
'gif' => 'image/gif',
);
return $mimes;
}

Comments  Leave a Reply

  1. Restrict WordPress Users from Uploading Audio & Video Media... December 14, 2011 at 1:01 pm

    […] the web that would do the same thing. And so if you prefer adding code to your functions file, then this code (from WPSnipp) will do the same thing as the plugin […]

  2. How would you set restriction parameters for non-image file types like PDFs? Thanks!

    1. Hi Rone, you should be able to just add a new line after line 05

      ‘gif’ => ‘image/gif’,
      ‘pdf’ => ‘application/pdf’,

      that should do it just copy the line and paste it in.

      1. Most excellent! Thanks Kevin!

        1. No problem anytime Rone,

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!