Using WordPress ‘media_upload_mime_type_links’ PHP filter

The media_upload_mime_type_links WordPress PHP filter allows you to modify the media upload mime type list items displayed in the WordPress Media Library.

Usage

add_filter('media_upload_mime_type_links', 'your_custom_function_name');
function your_custom_function_name($type_links) {
  // your custom code here
  return $type_links;
}

Parameters

  • $type_links (string[]): An array of list items containing mime type link HTML. Each value should begin with an <li> tag.

More information

See WordPress Developer Resources: media_upload_mime_type_links

Examples

Add a new mime type to the list

Add a new mime type (e.g., “SVG”) to the media upload mime type list.

add_filter('media_upload_mime_type_links', 'add_svg_mime_type');
function add_svg_mime_type($type_links) {
  $type_links[] = '<li><a href="#" data-mime-type="image/svg+xml">SVG</a></li>';
  return $type_links;
}

Remove a mime type from the list

Remove a specific mime type (e.g., “Video”) from the media upload mime type list.

add_filter('media_upload_mime_type_links', 'remove_video_mime_type');
function remove_video_mime_type($type_links) {
  foreach ($type_links as $key => $value) {
    if (strpos($value, 'data-mime-type="video"') !== false) {
      unset($type_links[$key]);
    }
  }
  return $type_links;
}

Modify the display text for a mime type

Change the display text for a specific mime type (e.g., “Image” to “Pictures”).

add_filter('media_upload_mime_type_links', 'modify_image_display_text');
function modify_image_display_text($type_links) {
  foreach ($type_links as $key => $value) {
    if (strpos($value, 'data-mime-type="image"') !== false) {
      $type_links[$key] = str_replace('Image', 'Pictures', $value);
    }
  }
  return $type_links;
}

Add a custom CSS class to a mime type

Add a custom CSS class to a specific mime type (e.g., “Audio”) in the list.

add_filter('media_upload_mime_type_links', 'add_css_class_to_audio');
function add_css_class_to_audio($type_links) {
  foreach ($type_links as $key => $value) {
    if (strpos($value, 'data-mime-type="audio"') !== false) {
      $type_links[$key] = str_replace('<a ', '<a class="custom-audio-class" ', $value);
    }
  }
  return $type_links;
}

Rearrange the order of mime types

Rearrange the order of mime types in the list (e.g., move “Video” to the first position).

In this example, we search for the “Video” mime type in the `$type_links` array, store its key and value, then remove it from the array. After that, we add the “Video” mime type back to the beginning of the `$type_links` array using `array_unshift()`. Finally, we return the modified `$type_links` array.

add_filter('media_upload_mime_type_links', 'reorder_mime_types');
function reorder_mime_types($type_links) {
  $video_key = null;
  $video_value = null;

  foreach ($type_links as $key => $value) {
    if (strpos($value, 'data-mime-type="video"') !== false) {
      $video_key = $key;
      $video_value = $value;
      break;
    }
  }

  if ($video_key !== null) {
    unset($type_links[$video_key]);
array_unshift($type_links, $video_value);
}

return $type_links;
}