The media_upload_default_type WordPress PHP filter allows you to change the default media upload type in the legacy (pre-3.5.0) media popup.
Usage
add_filter('media_upload_default_type', 'your_custom_media_upload_default_type'); function your_custom_media_upload_default_type($type) { // your custom code here return $type; }
Parameters
$type
string – The default media upload type. Possible values include ‘image’, ‘audio’, ‘video’, ‘file’, etc. Default ‘file’.
More information
See WordPress Developer Resources: media_upload_default_type
Examples
Set default media upload type to image
This example sets the default media upload type to ‘image’.
add_filter('media_upload_default_type', 'set_default_upload_to_image'); function set_default_upload_to_image($type) { $type = 'image'; return $type; }
Set default media upload type to audio
This example sets the default media upload type to ‘audio’.
add_filter('media_upload_default_type', 'set_default_upload_to_audio'); function set_default_upload_to_audio($type) { $type = 'audio'; return $type; }
Set default media upload type to video
This example sets the default media upload type to ‘video’.
add_filter('media_upload_default_type', 'set_default_upload_to_video'); function set_default_upload_to_video($type) { $type = 'video'; return $type; }
Set default media upload type based on user role
This example sets the default media upload type based on the current user’s role. If the user is an ‘editor’, it sets the default to ‘image’. Otherwise, it sets the default to ‘file’.
add_filter('media_upload_default_type', 'set_default_upload_based_on_role'); function set_default_upload_based_on_role($type) { if (current_user_can('editor')) { $type = 'image'; } else { $type = 'file'; } return $type; }
Set default media upload type depending on the current post type
This example sets the default media upload type based on the current post type. If the current post type is ‘gallery’, it sets the default to ‘image’. Otherwise, it sets the default to ‘file’.
add_filter('media_upload_default_type', 'set_default_upload_based_on_post_type'); function set_default_upload_based_on_post_type($type) { global $post; if ($post->post_type == 'gallery') { $type = 'image'; } else { $type = 'file'; } return $type; }