Created
March 8, 2015 16:24
-
-
Save cfxd/b4f1448519cb507f65fc to your computer and use it in GitHub Desktop.
Restrict the WordPress Media Library selection modal to images of a certain width (or by any other metadata). See http://wordpress.stackexchange.com/questions/180500/filter-media-library-items-by-size/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function restrict_media_library_by_width($query) { | |
$include = array(); | |
$exclude = array(); | |
$temp_query = new WP_Query($query); | |
if($temp_query->have_posts()) { | |
while($temp_query->have_posts()) { | |
$temp_query->the_post(); | |
$meta = wp_get_attachment_metadata(get_the_ID()); | |
$meta['mime-type'] = get_post_mime_type(get_the_ID()); | |
if(isset($meta['mime-type']) && | |
($meta['mime-type'] == 'image/jpeg' && isset($meta['width']) && $meta['width'] >= 100) || | |
$meta['mime-type'] == 'image/svg+xml') { | |
$include[] = get_the_ID(); | |
} else { | |
$exclude[] = get_the_ID(); | |
} | |
} | |
} | |
wp_reset_query(); | |
$query['post__in'] = $include; | |
$query['post__not_in'] = $exclude; | |
return $query; | |
} | |
add_filter('ajax_query_attachments_args', 'restrict_media_library_by_width'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment