Skip to content

Instantly share code, notes, and snippets.

@cfxd
Created March 8, 2015 16:24
Show Gist options
  • Save cfxd/b4f1448519cb507f65fc to your computer and use it in GitHub Desktop.
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/
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