Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the wordpress media uploader for uploading images in the theme that I created. But I want to restrict it to only images. I found out that there's this upload_mimes filter which can be used to restrict the media uploader to only specific types of files. But the problem with this approach is that its limiting all of the pages even when creating new posts to just images:

function mytheme_restrict_mime($mimes){

  $screen = get_current_screen();
  $current_page = $screen->base;


    $mimes = array(
      'jpg|jpeg|jpe' => 'image/jpeg',
      'gif' => 'image/gif',
      'png' => 'image/png'
    );


  return $mimes;
}

add_filter('upload_mimes', 'mytheme_restrict_mime');

So I tried getting the name of the current page:

function mytheme_restrict_mime($mimes){

  $screen = get_current_screen();
  $current_page = $screen->base;

  if($current_page == 'toplevel_page_mytheme-settings'){
    $mimes = array(
      'jpg|jpeg|jpe' => 'image/jpeg',
      'gif' => 'image/gif',
      'png' => 'image/png'
    );
  }

  return $mimes;
}

But this doesn't work. It allows me to upload any file type. Any ideas?

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.